简体   繁体   English

锚点在可点击的 div 内

[英]anchor inside a clickable div

I have this problem with a click script I have connected to a div.我连接到 div 的点击脚本有这个问题。 The thing I'm trying to accomplish is that when you click on an URL inside the clickable div, the click event wont be called, and you will be directed to whatever the anchor is calling.我要完成的事情是,当您单击可单击 div 内的 URL 时,不会调用单击事件,并且您将被定向到锚点调用的任何内容。

This is the JS这是JS

<script type="text/javascript">
$(document).ready(function()
{
    $(".comment_button").click(function(){

    var element = $(this);
    var I = element.attr("id");

    $("#slidepanel"+I).slideToggle(300);
    $(this).toggleClass("active");

    return false;
    });
});
</script>

And this is the html这是 html

<div class="comment_button" id="<?=$klotter_info['id']?>" style="cursor:pointer;">
<?=sanitize($klotter_info['message'])?> // Kommer i vissa fall ha länkar i sig
</div>

Any help is greatly appreciated.任何帮助是极大的赞赏。 Thanks in advance.提前致谢。

make click handler for your anchor and call stopPropagation:为您的锚点制作点击处理程序并调用 stopPropagation:

$('a').click(function(event){
   event.stopPropagation();
});

This will disable event bubbling.这将禁用事件冒泡。 and div click won't be triggered.并且不会触发 div 点击。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM