在没有"e"的情况下,有没有一种方法可以阻止javascript中的气泡范围? [英] Is there a way stop bubble in javascript without "e" parameter?

查看:77
本文介绍了在没有"e"的情况下,有没有一种方法可以阻止javascript中的气泡范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过很多教程,它们告诉我防止气泡的方法是使用"e"参数

I have see a lot tutorial that tell me the way to prevent bubble is use "e" parameter

就像:

function(e){
    e.preventDefault()
}

但是在某些情况下,萤火虫告诉我"e not define"是错误的

but in some situation,the firebug tell me it is wrong with "e is not define"

有没有办法在没有参数e的情况下停止气泡?

so is there a way to stop the bubble without the parameter e?

推荐答案

如果使用元素属性(例如<button onclick="myFunc()">)设置事件处理程序,则参数列表将为空.您必须使用<button onclick="myFunc(event)">代替将当前事件作为参数传递.默认情况下,不会将任何参数传递给回调函数.

If you set an event handler by using an element attribute (say, <button onclick="myFunc()">) the argument list will be empty. You have to use <button onclick="myFunc(event)"> instead to pass the current event as an argument. No argument will be passed to the callback function by default.

event是一个特殊变量.使用<element onEVENT="code">时,浏览器将使用FunctionBody代码"创建一个新脚本和一个函数.然后,此函数将使用一个参数event,因此您可以在自己的函数/代码中使用此对象(请参见

event is a special variable in this case. When using <element onEVENT="code"> the browser creates a new script and a function with the FunctionBody "code". This function will then take one argument event, thus you can use this object in your own function/code (see w3c:ehca). Note that IE creates a global object event for every triggered event.

因此,将event作为附加变量传递,并使用 e.preventDefault() e.stopPropagation .请注意,return false;不会取消click事件中的传播.

So pass event as an additional variable and use e.preventDefault() and e.stopPropagation. Note that return false; won't cancel the propagation in a click event.

演示

  • Demonstration showing the arguments of the callback function when using jQuery, <element onclick=""> and HTMLElementObject.onclick.
  • return false; vs e.stopPropagation(); (spoiler: return false; fails.)

参考文献:

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆