2016-05-07 30 views
1

我一直在嘗試創建一個主要使用PHP的流行語賓果遊戲。我從一個在特定網站上找到的25個單詞的數組中爲關鍵字添加一個特定的類。現在我只想使用jquery對所有單元格中的行進行樣式設置,但我無法弄清楚,這是我目前所掌握的。只有當所有的td都具有某個類時,如何才能將類添加到tr?

$('td.check').parent('tr').addClass('bingo'); 

PHP:

function bingo() { 

$buzzwords = array(
    "kaos", 
    "mega", 
    "super", 
    "kris", 
    "tragedi", 
    "döds", 
    "succé", 
    "avslöjar", 
    "chocken", 
    "terror", 
    "attack", 
    "mardröm", 
    "rekord", 
    "galen", 
    "knark", 
    "attentat", 
    "extrem", 
    "kollaps", 
    "kränkt", 
    "skräll", 
    "myten", 
    "problem", 
    "varning", 
    "extra", 
    "besked" 
); 

shuffle($buzzwords); 

$inAb = array(); 


$bingocard = "<table class='tabell'>"; 
$bingocard .= "<thead><tr>"; 
$bingocard .= "<th>B</th> 
     <th>I</th><th>N</th> 
     <th>G</th><th>O</th>"; 
$bingocard .= "</tr></thead>"; 
$bingocard .= "<tbody>"; 
$bingocard .= "<tr>"; 

for($cell=0; $cell<25; $cell++) 
    { 

    $rowend = ($cell + 1) % 5; 
    $homepage = file_get_contents('http://www.example.com/'); 
     $value = $buzzwords[$cell]; 
     $antal = substr_count($homepage, $buzzwords[$cell]); 
     if ($antal > 0) { 
    $bingocard .= "<td class='check' style='color: red;'>" 
    . $buzzwords[$cell] . "</td>"; 
    array_push($inAb,$buzzwords[$cell]); 
     } 
    else { 
    $bingocard .= "<td>" 
    . $buzzwords[$cell] . "</td>"; 
    } 

    if($rowend == 0 && $cell < 24) { 
     $bingocard .= "</tr><tr>"; 
    } 
    } 

$bingocard .= "</tr>"; 
$bingocard .= "</tbody>"; 
$bingocard .= "</table>"; 
echo $bingocard; 
} 

bingo(); 

回答

2

檢查的td S的是tr兒童,這個數字是相同作爲td s的check類是相同tr的子女數:

if ($("tr").find("td").length == $("tr").find("td.check").length) { 
    $("tr").addClass("check"); 
} 

此外,使用each,以測試你的表.tabell的每tr

$(".tabell").find("tr").each(function(idx) { 
    var tr$ = $(this); 
    if (tr$.find("td").length == tr$.find("td.check").length) { 
     tr$.addClass("check"); 
    } 
}); 

更新: 因爲trth沒有td(0),無td.check(0也行),你應該跳過莫名其妙第一行。也許像

$(".tabell tbody").find("tr")` 

0 < $("tr").find("td").length && $("tr").find("td").length == $("tr").find("td.check").length 
+0

這幾乎做到了,謝謝! 由於某種原因,它也將課程添加到了我的課堂,儘管你知道爲什麼嗎? – oceansmoving

+0

已更新的答案。 –

+0

優秀,非常有幫助! – oceansmoving

0
$("table tr").each(function() { 
    var rowCells = $(this).children("td"); 
    var rowLength = rowCells.length; 
    var checkedCells = 0; 
    $.each(rowCells,function(v) { 
    if($(v).hasClass("check")) { 
    checkedCells++; 
    } 
    }); 
    if(checkedCells==rowLength) { 
    $(this).addClass('bingo'); 
    } 
}); 
1

白色很容易:

$("tr").filter(function() { 
      return ! $(this).children("td").not(".check").length 
     }).addClass("check") 

demo

相關問題