使用 Google Closure库:
如何处理点击element
例如div
,但阻止在用户点击该元素的子元素时触发event handler
。
例如,在以下代码中,我想在用户点击event handler
时触发div1
但是当他/她点击“span1”时,我希望调用另一个event handler
而不触发div1
的处理程序。
<div style="width: 400px" id="div1">
<span id="span1">click me to reload</span>
click here to Toggle accordion
</div>
更新
JS代码:
/**** accordion ***/
var panels = goog.dom.getElementsByClass('newsColumnHeader', goog.dom.getElement('accordionContainer'));
var anims = {};
var open = null;
goog.array.forEach(panels, function(pane){
var animation = new goog.ui.AnimatedZippy(pane, goog.dom.getNextElementSibling(pane));
goog.events.listen(animation, goog.ui.Zippy.Events.TOGGLE, zippyToggle);
anims[goog.getUid(animation)] = animation;
});
function zippyToggle(event) {
var uid = goog.getUid(event.target);
// simple logic - only one open panel in one time
if (event.expanded && uid != open) {
if (open) {
anims[open].setExpanded(false);
}
open = uid;
}
}
/******************/
var refreshVarzesh3 = goog.dom.getElement("btnRefreshVarzesh3");
if (refreshVarzesh3 != null) {
goog.events.listen(refreshVarzesh3, goog.events.EventType.CLICK, function(event) {
/*doing something but not toggling accordion pane*/
});
}
HTML CODE:
<body>
<div class="main">
<div class="top">
<div id="toolbar">
<img src="css/img/contact.png" alt="تماس، پیشنهاد، گزارش خطا" title="تماس، پیشنهاد، گزارش خطا"/>
</div>
<img src="css/img/football_news.gif" alt="آخرین اخبار فوتبال"/>
</div>
<div class="middle">
<div class="left"></div>
<div class="right">
<div class="accordion" id="accordionContainer">
<div class="newsColumnHeader">
<div class="buttons">
<img id="btnRefreshVarzesh3" src="css/img/refresh.png" alt="به روز رسانی" title="به روز رسانی"/>
</div>
<%=lbls.getString("Varzesh3News")%>
</div>
<div class="newsList" id="varzesh3NewsContainer"></div>
<div class="newsColumnHeader"><%=lbls.getString("PersepolisNews")%></div>
<div class="newsList" id="persepolisNewsContainer"></div>
<div class="newsColumnHeader"><%=lbls.getString("EsteghlalNews")%></div>
<div class="newsList" id="esteghlalNewsContainer"></div>
<div class="newsColumnHeader"><%=lbls.getString("NavadNews")%></div>
<div class="newsList" id="navadNewsContainer"></div>
</div>
</div>
</div>
<div class="bottom"></div>
</div>
答案 0 :(得分:9)
对于纯javascript 开发人员,答案是here,但如果您使用 Google Closure Library ,则以下代码就足够了:
event.stopPropagation();
答案 1 :(得分:4)
在该点击的事件处理程序中,您必须使用preventDefault()
例如:
document.getElementById('div1').onclick = function (event) {
//your code
event.preventDefault();
}
答案 2 :(得分:2)
您应该阅读此链接以了解有关活动的更多信息 http://www.quirksmode.org/js/events_order.html
答案 3 :(得分:1)
仅供参考Google Closure中的所有事件均来自goog.events.Event
http://docs.closure-library.googlecode.com/git/class_goog_events_Event.html
所以第一个一个是stopPropagation
this.getHandler().listen(element ,goog.events.EventType.CLICK, function (e){
e.stopPropagation();
});
第二一个是goog.events.Event.stopPropagation
是静态方法
反过来调用上面的方法
goog.events.Event.stopPropagation = function(e) {
e.stopPropagation();
};
编辑: 请阅读 - Dangers of Event Propagation