Javascript 在div上的锚标记内部单击时停止单击事件传播

Javascript 在div上的锚标记内部单击时停止单击事件传播,javascript,jquery,Javascript,Jquery,说明: $(".add_this_person").on("click", "a", function (e) { e.preventDefault(); } ); // where add_this_person is a class of both divs 我有下面的HTML <a href = "go some where"> <div onclick = "myfunc1(....)">CLICK ME A</div> <div

说明:

$(".add_this_person").on("click", "a", function (e) {
    e.preventDefault();
} ); // where add_this_person is a class of both divs
我有下面的HTML

<a href = "go some where">

<div onclick = "myfunc1(....)">CLICK ME A</div>

<div onclick = "myfunc2(....)">CLICK ME B</div>

</a>
您可以尝试以下方法:-
class
添加到div和change
jQuery
如下代码:-

$(".add_this_person").on("click", "a", function (e) {
  e.preventDefault();
  if($(e.target).is('.first'))
  {
     alert("A");
  }else if($(e.target).is('.second'))
  {
    alert("B");
  }else{
    window.location=$(this).attr('href');
  }
 }); 

您的尝试没有问题,但是您可以使用jQuery的全部潜力,而不必在div中使用“onclick”事件。只需分配一个类并生成一个类事件,该类事件将为具有给定类的所有元素触发,并将目标函数存储在一个额外的属性中

只需将div标记放在action“my action”上,并将目标函数存储在 以函数名为目标的HTML数据主干“函数”

<div class="my-action" data-function="myAction1" >CLICK ME A</div>

<div class="my-action" data-function="myAction2">CLICK ME B</div>


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

        //Declare your functions here, add them to window[] array...
        window.myAction1 = function() {
            alert("A");
        }

        window.myAction2 = function() {
            alert("B");
        }

        //This will trigger whenever an element with class "my-action" is clicked...
        $(document).on("click", ".my-action", function (e) {
            //Just read the function name from data-function and assign it from window[] array
            var targetFunction = window[$(this).data("function")];

            //And execute it, done!
            targetFunction();
        });
    });
</script>
点击我一个
点击我B
$(文档).ready(函数(){
//在此处声明函数,将它们添加到window[]数组。。。
window.myAction1=函数(){
警报(“A”);
}
window.myAction2=函数(){
警报(“B”);
}
//每当单击类为“my action”的元素时,将触发此操作。。。
$(文档)。在(“单击”,“我的操作”,函数(e){
//只需从数据函数中读取函数名,并从window[]数组中分配它
var targetFunction=窗口[$(this).data(“function”);
//然后执行它,完成!
targetFunction();
});
});
创造了一个小提琴作品。。。