如何使用内联onclick属性停止事件传播?

Sam*_*Sam 297 html javascript events dom

考虑以下:

<div onclick="alert('you clicked the header')" class="header">
  <span onclick="alert('you clicked inside the header');">something inside the header</span>
</div>
Run Code Online (Sandbox Code Playgroud)

如何才能使用户点击跨度时,它不会触发div点击事件?

Jam*_*mes 251

使用event.stopPropagation().

<span onclick="event.stopPropagation(); alert('you clicked inside the header');">something inside the header</span>
Run Code Online (Sandbox Code Playgroud)

对于IE: window.event.cancelBubble = true

<span onclick="window.event.cancelBubble = true; alert('you clicked inside the header');">something inside the header</span>
Run Code Online (Sandbox Code Playgroud)

  • 这是错误的 - 内联onclick处理程序不会将事件作为参数传递.正确的解决方案是Gareths,下面. (64认同)
  • 在FireFox中没有事件对象这样的东西. (11认同)
  • 事件对象是回调的参数.实际上,IE中没有事件对象,因为这个对象可以通过window.event访问,而不是函数的参数:-) (6认同)
  • 在Firefox中,您可以访问内联脚本中的变量事件,但window.event不可用.<div onclick ="alert(event);"> </ div> (2认同)

Gar*_*eth 208

有两种方法可以从函数内部获取事件对象:

  1. 第一个参数,符合W3C标准的浏览器(Chrome,Firefox,Safari,IE9 +)
  2. Internet Explorer中的window.event对象(<= 8)

如果您需要支持不遵循W3C建议的旧版浏览器,通常在函数内部使用以下内容:

function(e) {
  var event = e || window.event;
  [...];
}
Run Code Online (Sandbox Code Playgroud)

这将检查第一个,然后另一个并存储在事件变量中找到的任何一个.但是在内联事件处理程序中没有e要使用的对象.在这种情况下,您必须利用arguments始终可用的集合并引用传递给函数的完整参数集:

onclick="var event = arguments[0] || window.event; [...]"
Run Code Online (Sandbox Code Playgroud)

但是,一般来说,如果你需要处理像停止传播这样复杂的事情,你应该避免使用内联事件处理程序.分别编写事件处理程序并将它们附加到元素是中长期更好的想法,无论是为了可读性还是可维护性.

  • 从内联侦听器,您可以传递事件对象,如:`onclick ="foo(event)"`,然后在函数`function foo(event){/*do stuff with event*/}`.这适用于IE和W3C事件模型. (10认同)
  • 这实际上并没有回答这个问题. (8认同)
  • "小于或等于八"有点......含糊不清. (5认同)

Rob*_*rth 80

请记住,FireFox不支持window.event,因此它必须是以下内容:

e.cancelBubble = true
Run Code Online (Sandbox Code Playgroud)

或者,您可以使用FireFox的W3C标准:

e.stopPropagation();
Run Code Online (Sandbox Code Playgroud)

如果你想获得幻想,你可以这样做:

function myEventHandler(e)
{
    if (!e)
      e = window.event;

    //IE9 & Other Browsers
    if (e.stopPropagation) {
      e.stopPropagation();
    }
    //IE8 and Lower
    else {
      e.cancelBubble = true;
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 为了使它工作,人们需要这样称呼:`<div onclick ="myEventHandler(event);">` (12认同)

Sof*_*ARM 45

使用此功能,它将测试是否存在正确的方法.

function disabledEventPropagation(event)
{
   if (event.stopPropagation){
       event.stopPropagation();
   }
   else if(window.event){
      window.event.cancelBubble=true;
   }
}
Run Code Online (Sandbox Code Playgroud)


小智 18

我有同样的问题 - IE中的js错误框 - 就我所见,这在所有浏览器中都能正常工作(event.cancelBubble = true可以在IE中完成工作)

onClick="if(event.stopPropagation){event.stopPropagation();}event.cancelBubble=true;"
Run Code Online (Sandbox Code Playgroud)

  • 在这种情况下,内联JS是完全不可读的:( (2认同)

Mat*_*man 10

这对我有用

<script>
function cancelBubble(e) {
 var evt = e ? e:window.event;
 if (evt.stopPropagation)    evt.stopPropagation();
 if (evt.cancelBubble!=null) evt.cancelBubble = true;
}
</script>

<div onclick="alert('Click!')">
  <div onclick="cancelBubble(event)">Something inside the other div</div>
</div>
Run Code Online (Sandbox Code Playgroud)


Evg*_*orb 7

对于ASP.NET网页(不是MVC),您可以使用Sys.UI.DomEventobject作为本机事件的包装器.

<div onclick="event.stopPropagation();" ...
Run Code Online (Sandbox Code Playgroud)

或者,将事件作为参数传递给内部函数:

<div onclick="someFunction(event);" ...
Run Code Online (Sandbox Code Playgroud)

在某些功能:

function someFunction(event){
    event.stopPropagation(); // here Sys.UI.DomEvent.stopPropagation() method is used
    // other onclick logic
}
Run Code Online (Sandbox Code Playgroud)


小智 6

由于业力,我无法发表评论,所以我将其写成完整的答案:根据 Gareth (var e = arguments[0] || window.event; [...]) 的回答,我在 onclick 上使用了此 oneliner inline for a快速破解:

<div onclick="(arguments[0] || window.event).stopPropagation();">..</div>
Run Code Online (Sandbox Code Playgroud)

我知道现在已经晚了,但我想让你知道这在一行中有效。大括号返回一个事件,该事件在两种情况下都附加了 stopPropagation 函数,因此我尝试将它们封装在大括号中,例如 if 和....它可以工作。:)


Cod*_*ife 6

使用单独的处理程序,例如:

function myOnClickHandler(th){
//say let t=$(th)
}
Run Code Online (Sandbox Code Playgroud)

并在 html 中执行以下操作:

<...onclick="myOnClickHandler(this); event.stopPropagation();"...>
Run Code Online (Sandbox Code Playgroud)

甚至 :

function myOnClickHandler(e){
  e.stopPropagation();
}
Run Code Online (Sandbox Code Playgroud)

为了:

<...onclick="myOnClickHandler(event)"...>
Run Code Online (Sandbox Code Playgroud)


ajh*_*138 5

根据这个页面,在IE中你需要:

event.cancelBubble = true