gpt4 book ai didi

javascript - 在 jQuery 中编写更好的选择器

转载 作者:行者123 更新时间:2023-11-30 09:05:11 26 4
gpt4 key购买 nike

编写以下选择器的最佳方式是什么?

id = "#id",
$id = $("#id");
//works but seems wrong
$($id, 'a').click(function(){
//do stuff..
});

//this was not working for me.
$id.find('a').click(function(){
// do stuff...
});

最佳答案

较短查找的正确语法是:

$('a', $id).click(function(){
//do stuff..
});

这会找到 $id 中的所有 anchor 标记>

将它与 find fn 一起使用:

$id.find('a').click(function(){
// do stuff...
});

这一切都假设你的标记是这样的:

<div id='id'> //div or any other html element
... any code
<a href.... ></a> << the anchor can be anywhere inside this div
</div>

关于javascript - 在 jQuery 中编写更好的选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5600242/

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