[英]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 : 我的html代码是:
<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 但是当我单击Childe 2.1时,它无法打开google.com
Please help me. 请帮我。
Thanks 谢谢
I take it from... 我从...拿走
but when i click on Childe 2.1 it does open google.com 但是当我单击Childe 2.1时,它确实会打开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. 如果这不是您要的内容,请扩展您的OP,以包括有关您的问题以及您希望发生的事情的更多详细信息。
I think this is what you want. 我想这就是你想要的。 The e.stopPropagation()
is what I added. 我添加了e.stopPropagation()
。
$('.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 function function(e)
. 注意,我还向函数function(e)
添加了e
作为参数。
Prevents the event from bubbling up the DOM tree, preventing any parent handlers from being notified of the event. 防止事件使DOM树冒泡,从而防止任何父处理程序收到该事件的通知。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.