开发者

Hyperlink in a div with jQuery.toggle Event

开发者 https://www.devze.com 2023-03-05 15:05 出处:网络
I have a DIV element which has a jQuery.toggle Event. If I add a hyperlink as content of this div, a click on this hyperlink will fire the jQuery.toggle Event of the parent div. Is it possible to prev

I have a DIV element which has a jQuery.toggle Event. If I add a hyperlink as content of this div, a click on this hyperlink will fire the jQuery.toggle Event of the parent div. Is it possible to prevent this? The hyperlink should just open the w开发者_运维百科eblink, and not fire the event.


Try with :

$("#linkInsideYourDiv").click(function(event)) {
    event.stopPropagation();
    // some other stuff
}


just call unbind on the $(this).parent when you are in the child toggle first

i.e.

$(this).parent().unbind("toggle");

Another thing you can do is call the avoid propogation method to avoid passing the toggle / click event etc back up the chain of bound listeners.

i.e.

$(this).click(function(e){
    // Do your click stuff

    // Prevent any further processing of the click event
    e.stopPropagation();
});
0

精彩评论

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