2016-05-07 6 views
1

Ich habe versucht, ein Buzzword Bingo hauptsächlich mit PHP zu erstellen. Ich füge den Keywords eine bestimmte Klasse aus einem Array von 25 Wörtern hinzu, die auf einer bestimmten Website zu finden sind. Jetzt möchte ich nur jquery verwenden, um die Zeile zu stylen, wenn alle Zellen diese Klasse haben, aber ich kann nicht herausfinden, wie, das ist alles, was ich im Moment habe.Wie Klasse nur tr hinzufügen, wenn alle tds eine bestimmte Klasse hat?

$('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(); 

Antwort

2

Überprüfen Sie, ob Anzahl der td s, die Kinder von tr sind die gleiche wie Anzahl der td s mit Klasse check die Kinder derselben tr sind:

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

Verwenden Sie außerdem each.tabell jeden tr Ihrer Tabelle testen:

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

aktualisieren: Da tr mit th hat keine td (0) und kein td.check (0 zu), sollten Sie irgendwie ersten Zeile überspringen. Vielleicht so etwas wie

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

oder

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

Dieses ziemlich tat es, danke! Aus irgendeinem Grund hat es die Klasse zu meinem Th hinzugefügt, obwohl, weißt du warum? – oceansmoving

+0

Aktualisierte Antwort. –

+0

Ausgezeichnet, sehr hilfreich! – 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

Weiß es einfach:

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

demo

Verwandte Themen