admin管理员组文章数量:1312763
i have the following structure
Code
<a href="blah.html" class="re-direct">
<div class="1div"></div>
<div class="2div">
<div class="click" data-id="text">click</div>
</div>
</a>
<script>
$(document).on('click',".click",function(){ alert($(this).data('id')) })
</script>
the above code works fine but it redirects to the page blah.html how do i stop it from redirecting ?
i have the following structure
Code
<a href="blah.html" class="re-direct">
<div class="1div"></div>
<div class="2div">
<div class="click" data-id="text">click</div>
</div>
</a>
<script>
$(document).on('click',".click",function(){ alert($(this).data('id')) })
</script>
the above code works fine but it redirects to the page blah.html how do i stop it from redirecting ?
Share Improve this question asked Jul 13, 2014 at 19:22 Dev ManDev Man 2,1373 gold badges24 silver badges40 bronze badges1 Answer
Reset to default 8You need to stop the propagation of the click event up the DOM to the a
element, and also prevent the default behaviour of the link:
$(document).on('click', ".click", function(e) {
e.stopPropagation();
e.preventDefault();
console.log($(this).data('id'))
})
<script src="https://cdnjs.cloudflare./ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<a href="blah.html" class="re-direct">
<div class="1div"></div>
<div class="2div">
<div class="click" data-id="text">click</div>
</div>
</a>
本文标签: javascripthow to stop redirection of parent href tag when clicked on child divStack Overflow
版权声明:本文标题:javascript - how to stop redirection of parent href tag when clicked on child div? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741843005a2400615.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论