[英]Writing better selector in jQuery
What would be the best way to write the following selector? 编写以下选择器的最佳方法是什么?
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...
});
the proper syntax for the shorter find is: 较短查找的正确语法是:
$('a', $id).click(function(){
//do stuff..
});
this finds all anchor tags within $id
这会找到$id
所有锚标签
to use it with the find fn: 与find fn一起使用:
$id.find('a').click(function(){
// do stuff...
});
this is all under the assumption that ur markup is like so: 所有这些都是在您的标记像这样的假设下进行的:
<div id='id'> //div or any other html element
... any code
<a href.... ></a> << the anchor can be anywhere inside this div
</div>
The context is the second argument , not the first. 上下文是第二个参数 ,而不是第一个。
$('a', '#id').click(function(){
//do stuff..
});
$("#id a").click(function() {} );
要么
$("#id > a").click(function() {} );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.