【发布时间】:2014-09-15 15:03:34
【问题描述】:
我正在尝试使用一些 BB-Code 控件来增强 HTML textarea,这些控件应该仅在 textarea 具有焦点时可用。所需的行为如下所示:
图 1 - 焦点事件前:
(来源:gaedekenet.de)
图 2 - 焦点事件后(正确行为):
(来源:gaedekenet.de)
上述正确行为是通过单击文本区域的下部来完成的 - 不会出现任何按钮。但是每当用户点击文本区域的左上角时,就会发生以下情况:
图 3 - 焦点事件后(不正确的行为):
(来源:gaedekenet.de)
在我看来,不仅触发了“focus”事件,还触发了“click”、“mousedown”、“mouseup”等。
我现在需要做的是在我自己的代码(焦点部分)中停止事件传播,以便没有事件可以到达 BB Code 插件(这是一个 3rd 方插件)。我目前的做法是这样的:
$(#myText)
.focus(function(e){
e.stopPropagation().stopImmediatePropagation();
// initialize and show the bb code buttons here
})
.click(function(e){
e.stopPropagation().stopImmediatePropagation();
})
.mouseup(function(e){
e.stopPropagation().stopImmediatePropagation();
})
.mousedown(function(e){
e.stopPropagation().stopImmediatePropagation();
});
我错过了什么?
【问题讨论】:
-
你试过 e.preventDefault() 吗?
标签: jquery event-bubbling stoppropagation