gpt4 book ai didi

javascript - 第 n 个 child 和 if else 语句

转载 作者:行者123 更新时间:2023-11-30 08:08:34 25 4
gpt4 key购买 nike

是否可以使用第 nth-child 和 if else 语句实现以下目标?

假设有 9 个 div,每个 div 中都有一个链接,单击该链接会执行一个功能。我能否以某种方式让链接找出它里面的第 nth-child div(来自 body 标签)并仅当它在第 3、6 或 9 个 div 中时才执行该功能?我真的不知道从哪里开始,但似乎有可能。

最佳答案

您可以使用 nth-clild ,

Live Demo

$('.divclass :nth-child(3n)')

对于绑定(bind)事件,可以使用click()

Live demo

$('#divId div:nth-child(3n) a').click(function(){
alert($(this).text());
});​

关于javascript - 第 n 个 child 和 if else 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13927048/

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