English 中文(简体)
在利用“ j”活动时有什么正确的想法。
原标题:What s the right mindset in using jQuery s event.stopPropagation()

我在如何使用j Query s event.stoppropagation()方面有两种做法:

  1. Optimistic approach - By default, all event handlers are without stopPropagation(). stopPropagation() would be added to a handler only if there s some unnecessary and undesired behaviour caused by event bubbling.
  2. Pessimistic approach - By default, stopPropagation() is added to all event handlers. If there s some desired behaviour missing due to disconnected even bubbling, then the corresponding stopPropagation() would get removed.

我很想知道,在我的发展中使用<条码>推广(<>)时,这两者之间究竟是哪一个,还是哪一个。 我对2点感动更多,因为它认为,由于无谓的泡沫被阻止,它会取得更好的业绩。 我愿听取关于这个问题的专家的意见。

最佳回答

电话stopPropagation 各地情况良好。 它打破了事件代表团的日程,除非你字面上将处理人员添加到网页上的每一部分(如果你再次这样做,你应该利用事件代表团),而且你可能不会忘记这样做。

So rule of thumb: Only call stopPropagation when you need to do so.


* E/CN.6/2009/1。 这就是.live,.delegate ,and the delegation edition of .on


这里有一个事件代表团的例子,它依靠布局来听取来自<><><>>后>内容的活动。 听众是:

document.documentElement.addEventListener( click , function(e) {
    if(e.target.nodeName ===  a ) {
        console.log( A link was clicked! );
    }
});

在这里,有些人 throw着 j子,打破了:

$( div ).click(function(e) {
    e.stopPropagation();
});

任何<代码><a> 是<代码><的后代;div>将不再处理其点击! 请不要这样做。

问题回答

电话:stoppropagation,但前提是您确实需要。 并且记住,如果你使用j Query live功能,并且你停止使用,活手永远不会打电话,因为live 存储于的文件





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签