开发者

jquery not select?

开发者 https://www.devze.com 2022-12-23 23:54 出处:网络
i have a click event 开发者_开发问答for $(\'#blah div\'). div has text inside of it (not inside a div, span, p, etc) and has a textarea in it. The textarea is triggering the event as well, how do i ma

i have a click event 开发者_开发问答for $('#blah div'). div has text inside of it (not inside a div, span, p, etc) and has a textarea in it. The textarea is triggering the event as well, how do i make it only trigger when i click the text and ignore the textarea?


Stop the bubble, like this:

$('#blah div').click(function() {
  alert("Div click");
});

$('#blah div textarea').click(function(e) {
  e.stopPropogation();
});

This tells it to cancel the event bubble when the click originated in the <textarea>. Normally the click happens on the <textarea> and continues to bubble to the parents and fire their click handlers...this prevents that.


An alternative to preventing the event from happening is dealing with it:

$("#blah div").bind("click", function(e) {
    if (e.target.type !== "textarea") {
        alert("Hello.");
    }
});

You can check what the target of the event was inside your event handler function. That means that you can take further actions only if the target is anything but a textarea.

0

精彩评论

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