Zhaiduo
Zhaiduo

Reputation: 49

How to use jquery to disable click event of a DIV within a link?

There's a link in the DIV

<div><a href="#">go</a></div>

Can I disable click event on the DIV, but still works on the link?

I use this for test, but seems not work:

$(this).click(function(event){
    var _self_link=$('a',this);
    if(event.target!=_self_link){dosomething}
});

Any solution with Jquery? Thanks.

Upvotes: 1

Views: 5016

Answers (3)

Huske
Huske

Reputation: 9296

Try something like this:

$('div:has(a[href="your_link"])').click(function(e) {
    stopPropagation();
});

You can test it here http://jsfiddle.net/cnzvL/13/

Upvotes: 4

Yauheni Leichanok
Yauheni Leichanok

Reputation: 1769

$('div').click(function(event){
    event.preventDefault();
    // and then do what you want
});

Edited: maybe I didn't understand the question correctly. Here is the second variant:

$('a').click(function(event){
    event.stopPropagation();
    // and then do what you want
});

Upvotes: 2

Kamyar
Kamyar

Reputation: 18797

I think you should use stopPropagation.

Stops the bubbling of an event to parent elements, preventing any parent handlers from being notified of the event.

Upvotes: 5

Related Questions