gpt4 book ai didi

javascript - JQuery HTML 避免每个表中的第一行

转载 作者:行者123 更新时间:2023-11-28 18:48:48 25 4
gpt4 key购买 nike


我的网站上有几个表,我正在尝试编写 2 个函数。
第一个功能是检查鼠标是否在该行上,然后设置该行的背景颜色。
第二个功能是检查鼠标是否离开一行,然后删除背景颜色。
但我试图避免每个表的第一行

HTML 代码:

<table>
<tr><td>Name</td></tr>
<tr><td>David</td></tr>
<tr><td>Lukas</td></tr>
</table>
<table>
<tr><td>Something Else</td></tr>
<tr><td>John</td></tr>
<tr><td>Pedro</td></tr>
</table>

JQuery 代码:

$(document).on('mouseenter', 'table tr:not(:first)', function () {
$(this).css("background", "orange");
});

$(document).on('mouseleave', 'table tr:not(:first)', function () {
$(this).css("background", "");
});

我必须使用“.on”,因为第二个表是通过 Ajax 请求加载的。

当我在第一个表格的第一行输入鼠标时,什么也不会发生(没关系!)
但是当我进入第二个表的第一行时,它就会改变

谢谢!

最佳答案

使用first-child而不是firstfisrt 仅选择第一个元素,而 first-child 选择所有第一个子元素。

$(document).on('mouseenter', 'table tr:not(:first-child)', function () {
$(this).css("background", "orange");
});

$(document).on('mouseleave', 'table tr:not(:first-child)', function () {
$(this).css("background", "");
});

关于javascript - JQuery HTML 避免每个表中的第一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34862097/

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