简体   繁体   English

event.stopPropagation()在Javascript中未结束冒泡

[英]event.stopPropagation() Not Ending Bubbling In Javascript

I am starting to learn how this bubbling works in Javascript. 我开始学习这种冒泡如何在Javascript中工作。 Now my problem is, I guess I don't understand it completely! 现在我的问题是,我想我不太了解! I am running an onclick in HTML (Which looks like this: 我正在HTML中运行onclick (看起来像这样:

onclick="checkboxhit(<?php echo $allmail[$key]["mailid"]; ?>)"

), and it is running both the If and Else statements. ),并且同时运行If和Else语句。 Basically I am just trying to check WHEN and WHEN NOT check boxs are clicked. 基本上,我只是尝试检查“ 何时”和“ 何时不”复选框。 Here is the Javascript I am using: 这是我正在使用的Javascript:

listofmailids = [];
function checkboxhit(mailid) {
    if (listofmailids.indexOf(mailid) == -1) {
        listofmailids.push(mailid);
        $("#deletemail").css("color", "#474747");
        event.stopPropagation()
    }
    else {
        listofmailids.splice(listofmailids.indexOf(mailid), 1);
        if (listofmailids.length == -1) {
            $("#deletemail").css("color", "#A3A3A3");
        }
        event.stopPropagation()
    }
}

Yet the event.stopPropagation() is not stopping both the If and Else statements from executing. 然而event.stopPropagation()并没有阻止If和Else语句的执行。 How could I fix this? 我该如何解决? Thank you! 谢谢!

When using onclick , if you want the event object, you need to explicitly pass it 使用onclick ,如果需要event对象,则需要显式传递它

onclick="checkboxhit(<?= json_encode($allmail[$key]['mailid']) ?>, event)"

and in your function 而在你的职能

function checkboxhit(mailid, e) {
    e.stopPropagation();
    // etc

json_encode() will ensure the $allmail[$key]['mailid'] value is safe for use as a JavaScript literal. json_encode()将确保$allmail[$key]['mailid']值可安全用作JavaScript文字。


If you want to stop propagation and prevent the default action, you can use a falsy return value like so 如果要停止传播阻止默认操作,则可以使用伪造的返回值,如下所示

onclick="return checkboxhit(...)"

and in your function 而在你的职能

return false;

return false is sufficient to stop bubbling as long as there is no JavaScript run-time error prior to reaching the false return. 只要在到达假返回之前没有JavaScript运行时错误,返回假就足以停止冒泡。

Explicit passing the event parameter could be done in a more reliable fashion: 可以通过更可靠的方式来显式传递事件参数:

<a id="test">...</a>
<script>
function checkboxhit(mailid){
  return function(e){
    if (e) e.stopPropagation;
    ...
  }
}
document.getElementById('test').onclick=checkboxhit('123');
</script>

The above code runs in IE as well. 上面的代码也可以在IE中运行。 The chosen answer will cause an error because "e" is not defined. 选择的答案将导致错误,因为未定义“ e”。

You should be using event listeners instead of inline handlers. 您应该使用事件侦听器,而不是内联处理程序。 The Event object is added automatically in that case... 在这种情况下,会自动添加Event对象。

Anyway, consider the following - it will behave exactly as you expect :) 无论如何,请考虑以下内容-它的行为将完全符合您的预期:)

<div id="outer">
  <button id="test">click me</button>
</div>

then... 然后...

document.getElementById('test').addEventListener('click', function (ev) {
  ev.stopPropagation()
}, false)

document.getElementById('outer').addEventListener('click', function (ev) {
  // never called...
}, false)

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

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