javascript - 单击 td 时更改 bgcolor?

标签 javascript jquery html css html-table

<分区>


这个问题是由于错别字或无法再重现的问题引起的。虽然类似的问题可能是on-topic在这里,这个问题的解决方式不太可能帮助 future 的读者。

关闭 7 年前

我在让我的 html 表更改单个 td `

时遇到了一些问题

$(document).ready(function(){
    $("#table1 > td").click(function(){
        $(this).toggleClass("active");
    });
});
table td.active {
    background: #006633;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<table width="100%" border="0" id="table1">
                    <tr>
                    <td colspan="6" height="20px" bgcolor="#CCCCCC"><strong>CULTURE</strong></td>
                    </tr>
               
                    <tr>
                    <td width="5%"><p align="center">1</p></td>
                    <td width="40%">Is the CDP obvious - You Said / We Did Boards; Feedback Stations; Posters?</td>
                    <td width="5%" height="20px"><p align="center">1</p></font></td>
                    <td width="5%" height="20px"><p align="center">2</p></font></td>
                    <td width="5%" height="20px"><p align="center">3</p></font></td>
                    <td width="5%" height="20px"><p align="center">4</p></font></td>
                    </tr>
    </table>

点击 td` 时。出于某种原因,当它被点击时它没有做任何事情。有人知道这是为什么吗?

最佳答案

$("#table1 > td")$("#table1 td"),但这会增加您选择的匹配度。所以,我建议添加一个独特的属性(我鼓励 data-*)。

$(document).ready(function(){
    /*
    $("#table1 td").click(function(){
        $(this).toggleClass("active");
    });
    */
    // Better
    $("#table1 td[data-toggle]").click(function(){
        $(this).toggleClass("active");
    });
});
table td.active {
    background: #006633;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<table width="100%" border="0" id="table1">
                    <tr>
                    <td colspan="6" height="20px" bgcolor="#CCCCCC"><strong>CULTURE</strong></td>
                    </tr>
               
                    <tr>
                    <td width="5%"><p align="center">1</p></td>
                    <td data-toggle width="40%">Is the CDP obvious - You Said / We Did Boards; Feedback Stations; Posters?</td>
                    <td data-toggle width="5%" height="20px"><p align="center">1</p></font></td>
                    <td data-toggle width="5%" height="20px"><p align="center">2</p></font></td>
                    <td data-toggle width="5%" height="20px"><p align="center">3</p></font></td>
                    <td data-toggle width="5%" height="20px"><p align="center">4</p></font></td>
                    </tr>
    </table>

关于javascript - 单击 td 时更改 bgcolor?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36516747/

上一篇:html - 无法删除网站的页脚链接

下一篇:javascript - 如何设置无法编辑 HTML 且 ID 不断变化的元素的占位符文本?

相关文章:

javascript - 当 html 和 body 的高度为 100% 时,ScrollY/ScrollTop 不起作用

jquery - 检查是否在 jquery 中未选中单选按钮

javascript - javascript中是否有任何内置函数可以使用类选择器或元素选择器选择最后一个元素?

JavaServlet : getting info from a DB and showing it on the screen

javascript - 使用 jQuery 显示/隐藏 Bootstrap Accordion 面板

html - 如何使用 Javascript 转换 html 中的重音字符?

javascript - highcharts 3d 散点图 : displaying categories

javascript - PHP json在JavaScript中自动排序

javascript - 无法在 ViewModel Knockout 中执行函数

html - RegEx 和 HTML : How to match an element "foo", 其中至少包含两个其他元素 "bar"? (否定前瞻断言)