Detect Anchor tag in Jquery
My anchor tag is not working
here is my code :
$('.tree div').click(function(){
var o = $(this);
o.children('div').slideToggle();
o.filter(".parent").toggleClass("expand");
return false;
});
my html code is :
<div class="tree">
<div class="parent">
Parent
<div class="parent">
Parent 1
<div>
<a href="http://google.com">Childe 2.1</a>
</div>
<div>
Childe 2.2
</div>
</div>
开发者_运维问答 <div class="parent">
Parent 2
<div>
one 3.1
</div>
<div>
one 3.2
</div>
</div>
</div>
<div class="parent">
Parent
<div class="parent">
parent 1
<div>
Childe 2.1
</div>
<div>
Childe 2.2
</div>
</div>
<div class="parent">
parent 2
<div>
Childe 2.1
</div>
<div>
Childe 2.2
</div>
</div>
</div>
</div>
but when i click on Childe 2.1 it doesn't open google.com
Please help me.
Thanks
I think this is what you want. The e.stopPropagation()
is what I added.
$('.tree div').click(function(e){
var o = $(this);
o.children('div').slideToggle();
o.filter(".parent").toggleClass("expand");
e.stopPropagation();
});
Note I also added
e
as an argument to the functionfunction(e)
.Prevents the event from bubbling up the DOM tree, preventing any parent handlers from being notified of the event.
I take it from...
but when i click on Childe 2.1 it does open google.com
... You want to prevent the link behaviour?
In which case you need the following code:
$(".tree A").click(function(e) {
e.preventDefault();
});
If this is not what you are asking, please expand your OP to include more details about your problem, and what you expect to happen.
精彩评论