【问题标题】:Checking if table cell blocks have data and if so change the styling检查表格单元格块是否有数据,如果有,则更改样式
【发布时间】:2017-08-13 00:19:03
【问题描述】:

我创建了一个表格,用作草稿板。有用户,然后他们必须选择玩家。我想知道用户是否选择了一个播放器,如果无论如何我可以用不同的颜色样式设置该单元格块的样式,以更显着地显示该块被选中。然后空块离开正常的样式。

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

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>

【问题讨论】:

  • 所以,你没有使用 jQuery 吗?
  • 我会用任何东西来完成它。我只是不知道这是否可能。

标签: javascript php css if-statement html-table


【解决方案1】:

希望对你有帮助

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

https://jsfiddle.net/7w3c384f/

【讨论】:

  • 我不想点击单元格。我只是希望它检查单元格是否已填充,然后应用不同的样式
  • 试试这个。$("td.draft_table_td").each(function(){ if($(this).text()!=""){ $(this).css("背景色","蓝色"); } });
  • 太棒了!另一个与此直接相关的问题。如果以这种方式扩展一点会怎样。如果某些表格单元格说Tom - ABob - B,有没有办法搜索单元格是否已填充,然后子搜索字母 A 和 B。然后像给 A 红色然后给 B蓝色?
【解决方案2】:

为已填充或空白的表格单元格添加一个额外的 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;
 }

【讨论】:

  • 这对我不起作用...foreach ($userPlayerStore as $userPlayer) { if (empty($userPlayer['player'.$playerNum])){ $extra_class = 'emptycell'; }else{ $extra_class = 'filledcell'; } echo '&lt;td class="draft_table_td"&gt;' . $userPlayer['player' . $playerNum] . '&lt;/td&gt;'; ...和css ....td.draft_table_td.emptycell{ background-color: #606060; color: #FFFFFF; width: 14%; text-align: center; border: 1px solid black; } td.draft_table_td.filledcell{ background-color: #606060; color: red; width: 14%; text-align: center; border: 1px solid black; }
  • Loopo添加的css你加了吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-11
  • 2022-11-11
  • 2013-07-24
  • 1970-01-01
相关资源
最近更新 更多