2

使用谷歌闭包库

如何处理element例如 a 的单击,但在用户单击该元素的子元素时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 代码:

  <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>

4

3 回答 3

9

对于纯 javascript开发人员,答案就在这里,但如果您使用Google Closure Library,则以下代码就足够了:

event.stopPropagation();
于 2011-06-27T07:42:03.713 回答
4

在该单击的事件处理程序中,您必须使用 preventDefault()

例如:

document.getElementById('div1').onclick = function (event) {
   //your code
   event.preventDefault();
}
于 2011-06-27T06:38:21.493 回答
1

仅供参考 Google Closure 中的所有事件均源自goog.events.Event

http://docs.closure-library.googlecode.com/git/class_goog_events_Event.html

例如:goog.events.BrowserEvents

所以一个是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();
};

编辑:请阅读 -事件传播的危险

于 2013-08-19T21:07:31.833 回答