我有点困惑jQuery如何通过DOM搜索。 它是否从根节点选择,类似于XPath /选择器,还是从当前选择,无论它们在哪里,类似于XPath //选择器?通过ID或类与jquery选择嵌套元素
我有以下设置:
<body>
<div id="contentSection">
//A bunch of nested DIVs follow
<div id="parentDIV">
<span>Selector1</span>
<select class="selector" id="first">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<span>Selector2</span>
<select class="selector" id="second">
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
</select>
</div>
</div>
<div id="someOtherDIVs> </div>
<div id="someOtherDIVs> </div>
</body>
现在,如果我尝试首先选择的内容部分,然后按类别或ID过滤器,我可以将一个事件处理程序中选择元素,但如果我尝试要直接查询元素,事件处理程序从不附加。
例如,这个工程:
$('#contentSection').change('.plotSelector', function(e)
但这些并不:
$('#first').change(function(e) ...
$('.selector').change(function(e) ...
有人能解释一下为什么?有没有一种方法可以从根中选择DOM中任何位置的任何元素?
使用jQuery 2.1.1
对我的作品https://jsfiddle.net/3Lanknz7/ – Gavin
..和我https://jsfiddle.net/markwill/0nunxzy5/2/ –