开发者

jquery selector must select an area without an element which is in the area

I use jquery click event for a div and the div area has an input element. Because t开发者_高级运维he input element is in the div, when I click input, the click function works, but I don't want this action. I looked at the selector page in Jquery's website, but I couldn't find this selector. How can I do this?


Use the nodeName property of the originating element to see where the event originated:

$('#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
});

If you want a more complex query (e.g. using jQuery pseudoselectors), you can use 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
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜