1
我使用jquery点击事件的div和div区域有一个输入元素。因为输入元素在div中,所以当我点击输入时,click函数可以工作,但我不想要这个动作。 我看着Jquery网站上的选择器页面,但找不到这个选择器。我怎样才能做到这一点?jquery选择器必须选择一个区域没有在该区域中的元素
我使用jquery点击事件的div和div区域有一个输入元素。因为输入元素在div中,所以当我点击输入时,click函数可以工作,但我不想要这个动作。 我看着Jquery网站上的选择器页面,但找不到这个选择器。我怎样才能做到这一点?jquery选择器必须选择一个区域没有在该区域中的元素
使用originating element的nodeName
属性,看看那里的事件起源:
$('#myDiv').click(function(event) {
if (event.target.nodeName.toLowerCase() === 'input') {
return; // ignore the event if it originated on an input element
}
// do the rest of your code
});
如果你想要一个更复杂的查询(例如使用jQuery pseudoselectors),你可以使用is
:
$('#myDiv').click(function(event) {
if ($(event.target).is('input[name="foo"]')) {
return; // ignore the event if it originated on an input element with the name foo
}
// do the rest of your code
});
谢谢@lonesomeday,它的工作原理。 :) – 2011-05-07 15:35:58