我可以使用 Javascript 刷新 Firefox 中的事件堆栈吗?
我的 HTML 中有一个标签层次结构,其中所有标签都包含 onclick
事件处理程序。 onclick
被推送到事件堆栈上,从叶节点一直到层次结构的根节点。我只想响应叶 onclick
事件。我可以刷新事件堆栈而不是使用标志吗?
例如:
<ul>
<li onclick="nada('1');"><a href="#1">1</a></li>
<li onclick="nada('2');"><a href="#2">2</a>
<ul>
<li onclick="nada('2.1');"><a href="#2.1">2.1</a></li>
<li onclick="nada('2.2');"><a href="#2.2">2.2</a></li>
<li onclick="nada('2.3');"><a href="#2.3">2.3</a></li>
</ul>
</li>
<li onclick="nada('4');"><a href="#4">4</a></li>
<li onclick="nada('5');"><a href="#5">5</a></li>
</ul>
使用此功能单击 2.2...
function nada(which)
{
alert(which);
}
...将产生“2.2”和“2”的两个警报。
我可以在 nada 函数中添加什么来消除“2”的警报?
I have a hierarchy of tags within my HTML which all contain onclick
event handlers. The onclick
is pushed onto the event stack from the leaf back through the root of the hierarchy. I only want to respond to the leaf onclick
event. Can I flush the event stack rather than using a flag?
For instance:
<ul>
<li onclick="nada('1');"><a href="#1">1</a></li>
<li onclick="nada('2');"><a href="#2">2</a>
<ul>
<li onclick="nada('2.1');"><a href="#2.1">2.1</a></li>
<li onclick="nada('2.2');"><a href="#2.2">2.2</a></li>
<li onclick="nada('2.3');"><a href="#2.3">2.3</a></li>
</ul>
</li>
<li onclick="nada('4');"><a href="#4">4</a></li>
<li onclick="nada('5');"><a href="#5">5</a></li>
</ul>
Clicking on 2.2 using this function...
function nada(which)
{
alert(which);
}
...will result in two alerts for '2.2' and '2'.
What could I add to the nada function to eliminate the alert for '2'?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要阻止事件向上冒泡到父元素,您必须将其告知
event
对象。在 IE 中,您设置event.cancelBubble= true
。在其他浏览器中,您可以调用event.stopPropagation()
。您可能还想关闭默认的链接跟踪操作,以便浏览器不会一直跳到顶部尝试跟踪不存在的锚链接,例如
#1
。在 IE 中,您设置event.returnValue= false
。在其他浏览器中,您可以调用event.preventDefault()
。event
对象可在 IE 上作为window.event
进行访问。在其他浏览器上,它被传递到事件处理函数中。将事件传递到对两者都有效的函数中的一种方法是:但是,将
onclick
事件放在它通常所属的a
元素上可能会更好。这有助于提高可访问性,因为a
元素将是可聚焦的并且可通过键盘操作。这意味着您不必担心父母的点击处理程序被调用。(如果需要,您可以将
a
设置为看起来像普通块。)然后,您还可以使用一些不显眼的脚本来删除多余的
onclick
链接:To stop the event bubbling up to parent elements you have to tell the
event
object about it. In IE, you setevent.cancelBubble= true
. In other browsers, you callevent.stopPropagation()
.You probably also want to turn off the default link-following action so that the browser doesn't keep jumping up to the top trying to follow the non-existing anchor links like
#1
. In IE, you setevent.returnValue= false
. In other browsers, you callevent.preventDefault()
.The
event
object is accessible aswindow.event
on IE. On other browsers, it is passed into the event handler function. A way to pass the event into a function that works on both is:However it would probably be better all round to put the
onclick
event on thea
element which it usually belongs. This helps for accessibility, as thea
element will be focusable and keyboard-operable. And it means you don't have to worry about parents' click handlers being called.(You can style the
a
to look like a plain block, if you want.)You can then also kick out the redundant
onclick
links with a bit of unobtrusive scripting: