gpt4 book ai didi

javascript - jQuery : update css on one table column based on attribute of another column

转载 作者:太空宇宙 更新时间:2023-11-04 01:27:03 24 4
gpt4 key购买 nike

这是我的 HTML 表格

<!DOCTYPE html>
<html>
<body>
<table>
<thead>
<th>Header1</th>
<th>Header2</th>
</thead>
<tbody>
<tr>
<td><input type="checkbox" id=0 /></td>
<td>TEXT1</td>
</tr>
<tr>
<td><input type="checkbox" id=1 checked/></td>
<td>TEXT2</td>
</tr>
</tbody>
</table>

</body>
</html>

please click here

我的目标是扫描表中的所有行(在页面加载后)并通过 jquery 更新第二列文本的 css(如链接所示)

例如,上面的表格应该按上面的方式呈现(删除元素 TEXT2)

我相信我们可以使用 jquery 的 css 方法设置元素的 css,如下所示

.css("text-decoration", "line-through")

但我不知道如何构建一个可以扫描表中所有行并执行 css 的 jquery 片段。

这在 jquery 中真的可行吗?

感谢任何帮助!!!

最佳答案

<强>1。初始化脚本

要在页面加载之后运行脚本,请考虑将.on() 方法与load 事件一起使用。

引用:.on() | jQuery API Documentation

<强>2。遍历指定元素:

要遍历所需的元素,请考虑将 .each() 方法与指定的 css 选择器 td > input 一起使用,以直接定位嵌套的 input td 元素的元素(从而绕过所有 input 元素的目标恰好嵌套在任何特定的 td 元素中,并且无需链接另一个 .find() 方法来指定所需的嵌套元素)。

引用:.each() | jQuery API Documentation

3.检查所需条件:

要验证所讨论的元素是否具有属性checked,请考虑使用条件语句,例如if(),以检查是否条件评估为 true
如果确实如此,请在条件语句 block 中使用 .css() 方法添加所需的内联样式

引用:.css() | jQuery API Documentation

代码片段演示:

$(window).on('load', function(){

$('td > input').each(function(){
if($(this).attr('checked')) {
$(this).parent().next().css('text-decoration', 'line-through');
}
});

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<thead>
<th>Header1</th>
<th>Header2</th>
</thead>
<tbody>
<tr>
<td><input type="checkbox" id="text0" /></td>
<td>TEXT1</td>
</tr>
<tr>
<td><input type="checkbox" id="text1" checked/></td>
<td>TEXT2</td>
</tr>
</tbody>
</table>

注意:

    如果值以整数开头,则
  1. id 选择器无效(数字/单位值),例如:id="0",值必须以a开头字符串,例如:id="text0"
  2. 用撇号包裹属性值,例如:id="text0"

关于javascript - jQuery : update css on one table column based on attribute of another column,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47854322/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com