2015-09-06 19 views
0

我创建了一张表格,我正在使用该表格作为草稿板。有用户和玩家必须选择。我想知道如果用户选择了一名玩家,我可以使用不同的颜色样式对该单元格进行样式设置,以更明显地显示该块被选中。然后空块离开正常样式。检查表格单元格块是否有数据,如果是这样,更改样式

我创建了一个小提琴来展示它的外观。

https://jsfiddle.net/zmggLd57/

这样在他们的名字我想知道,如果电池我可以让他们以不同的颜色。

<table class="draft_border_table"> 
<tr> 
    <th class="draft_table_number_th">RND</th> 
<?php 
// Output usernames as column headings 
$userResults = mysqli_query($con, 'SELECT * FROM user_players ORDER BY `id`'); 
while ($userPlayer = mysqli_fetch_array($userResults)) { 
    $userPlayerStore[] = $userPlayer; 
    echo '<th class="draft_table_th"><div>' . $userPlayer['username'] . '</div></th>'; 
} 
?> 
</tr> 
<?php 
// Output each user's player 1-14 in each row 
$totalPlayerNumbers = 14; 
for ($playerNum = 1; $playerNum <= $totalPlayerNumbers; $playerNum++) { 
    echo '<tr><td><div class="draftBorder">' . $playerNum . '</div></td>'; 
    foreach ($userPlayerStore as $userPlayer) { 
     echo '<td class="draft_table_td">' . $userPlayer['player' . $playerNum] . '</td>'; 
    } 
    echo '</tr>'; 
} 
?> 
</table> 
+0

这样,你是不是使用jQuery? –

+0

我会用任何东西来完成它。我只是不知道这是否可能。 – Paul

回答

1

我希望,它会帮助你

$("td.draft_table_td").each(function(){ 
if($(this).text()!=""){ 
$(this).css("background-color","blue"); 
} 
}); 

https://jsfiddle.net/7w3c384f/

+0

虽然我不想点击单元格。我只希望它检查单元格是否已填充,然后应用不同的样式 – Paul

+1

试试这个。$(“td.draft_table_td”)。each(function(){if(this).text()!= “”){ $(this).css(“background-color”,“blue”); } }); – Kavin

+0

太好了!与此直接相关的另一个问题。如果这样扩展了一下,会怎么样呢?如果某些表格单元表示“Tom-A”Bob-B“有什么方法可以搜索单元格是否已填满,然后再次搜索字母A和B.那么就像给A一个红色然后B一个蓝色一样? – Paul

0

为填充或空白表格单元格添加额外的css类。

... 
foreach ($userPlayerStore as $userPlayer) { 
    if (empty($userPlayer['player'.$playerNum])){ 
     $extra_class = 'emptycell'; 
    }else{ 
     $extra_class = 'filledcell'; 
    } 

    echo '<td class="draft_table_td '.$extra_class.'">' . 
      $userPlayer['player' . $playerNum] . '</td>'; 
} 

然后添加相应的造型在你的CSS

td.draft_table_td.emptycell { 
    background: red; 
} 
+0

它不是为我工作...'的foreach($ userPlayerStore为$ userPlayer){ \t \t如果(空($ userPlayer [ '玩家' $ playerNum。])){ \t \t $ extra_class = 'emptycell'; \t \t} else { \t \t $ extra_class ='filledcell'; \t \t} echo''。 $ userPlayer ['player'。 $ playerNum]。 '';'...和css ....'td.draft_table_td.emptycell { \t background-color:#606060; \t颜色:#FFFFFF; \t宽度:14%; \t text-align:center; \t边框:1px纯黑色; } td.draft_table_td.filledcell { \t background-color:#606060; \t颜色:红色; \t宽度:14%; \t text-align:center; \t边框:1px纯黑色; }' – Paul

+0

您是否添加了Loopo添加的css? – JeanMel

+0

是的,我确实补充了这一点。 – Paul

相关问题