选择没有ID或类名的子项
Selecting Children without ID or Class names
我正试图从两个具有类和/或id的父div中选择一个输入文本框。这样做的例子和方法太多了,我尝试了太多,以至于我不得不发布这个问题,以备其他人可能遇到这个问题时提供帮助。
代码
<div class="tblsearch">
<div id="ElementCount10_filter" class="dataTables_filter">
<label>
Search:
<input type="text">
</label>
</div>
<div id="ElementCount10_info" class="dataTables_info"></div>
<div class="clear"></div>
</div>
我尝试过以下几种:
$(this).parent(".dataTables_filter").children("label").children("input").click(function () { alert('we got here'); });
$(".tblsearch.dataTables_filter label > input").click(function () {
alert('hovered');
});
$(this).parent(".dataTables_filter").children("input").click(function () { alert('we got here'); });
我无法访问选择器。我需要检查是否有人点击了框并启动了一个功能。这看起来非常简单,我的语法中一定遗漏了一些东西。
提前感谢您的帮助!
fiddle
$('.dataTables_filter').on('click', 'input', function() {
alert('here we are!');
});
试试这个。如果你需要更具体的,你可以做input[type="text"]
之类的。
这使用事件委派(有关更多信息,请参阅@Methletics的链接)。因此,这将处理父级中任何input
上的任何单击。
第二个选择器的问题是类名之间没有空格,包括一个空格会使它像预期的那样工作,因为.tblsearch
类的<div>
是.dataTables_filter
类的<div>
的祖先
$(".tblsearch .dataTables_filter label > input")
http://jsfiddle.net/MxffP/
这是[DEMO]。
$(".dataTables_filter").children("label").children("input").click(function () { alert('we got here'); });
$(".dataTables_filter label > input").click(function () {
alert('hovered');
});
$(".dataTables_filter").find("input").click(function () { alert('we got here'); });
children
只向下搜索一级。要向下搜索多个级别,请改用find
。
http://api.jquery.com/find/
工作jsfiddle:
http://jsfiddle.net/Vs5zZ/2/
相关文章:
- 基于类似索引的queryString的Id选择器
- 哪个更快?按 ID 选择,或按索引选择
- 获取 HTML 5 音频控制文件位置并在不使用 id 选择器的情况下更新 src
- Jquery按id选择变量
- 使 ajax 请求和成功数据显示在 id <选择>选择>部分中
- 如何在jQuery的.on()方法中动态更新按钮的ID选择器
- Mootools提交表单仅在通过ID选择表单时有效
- 不能同时通过类和数字 ID 选择 jquery 元素
- Jquery语法错误,id选择器上的表达式无法识别
- JavaScript id 选择器不起作用?不知道出了什么问题
- 将 ID 选择器注入样式
- 如何使用 JQuery 按类和 id 选择非父元素
- Jquery id 选择器变量无法绑定到 onchange
- 如何使用子元素的 ID 选择父元素的第一个子元素
- jQuery ID 选择器仅适用于第一个元素
- JavaScript 将 body_0$ 添加到 ID 选择器中
- 使用隐藏字段 ID 选择器加载另一个字段中的文本
- 如何使用带有值的 ID 选择选项到另一个动态选择选项值
- jquery id 选择器的问题
- jQuery id 选择器在