简体   繁体   English

使用DOMSubtreeModified变异事件。 在jQuery中

[英]Using DOMSubtreeModified mutation event. in jQuery

I have used the following jQuery code on my page and everything works fine on chrome.But when I open up the respective page in firefox I get the Unresponsive Script Error. 我在我的页面上使用了以下jQuery代码,并且chrome上的一切正常。但是当我在firefox中打开相应的页面时,我得到了无响应的脚本错误。

I know as per DOM3 specification the mutation events have been deprecated. 我知道根据DOM3规范,变异事件已被弃用。 But Still if anyone could help me out here, I'll be obliged. 但是,如果有人能帮助我在这里,我将被迫。

jQuery('#term').on("DOMSubtreeModified",function(){
$("#term > .click-slide").click(function(){
            $(this).siblings().slideToggle();

            });
 });

Respective HTML is: 各自的HTML是:

<div class="btn-slide" id="term">
    <div class="click-slide">
      <button>Search Terms </button>
    </div>
    <div class="btn-box">
       <label><span>Novena</span></label>
    </div>
</div>

Well this might not be a suitable answer here since the question was about Mutation-events , and the one posted below is using MutationObserver but still I'm posting it as some may find this useful. 那么这可能不是一个合适的答案,因为问题是关于Mutation-events ,下面发布的是使用MutationObserver,但我仍然发布它,因为有些人可能觉得这很有用。

This is the alternative I used for DOMSubtreeModified event in case some nodes are being added in the DOM . 如果在DOM中添加了一些节点,这是我用于DOMSubtreeModified事件的替代方法。

var target = $( "#term" )[0];
// Create an observer instance
var observer = new MutationObserver(function( mutations ) {
   mutations.forEach(function( mutation ) {
       var newNodes = mutation.addedNodes; // DOM NodeList
       if( newNodes !== null ) { // If there are new nodes added

        //alert('something has been changed');

      }
   });    
});

// Configuration of the observer:
var config = { 
    attributes: true, 
    childList: true, 
    characterData: true 
};

// Pass in the target node, as well as the observer options
observer.observe(target, config);
// Later, you can stop observing
// observer.disconnect();

It looks like in Firefox, the call to .slideToggle() is triggering the DOMSubtreeModified event, while this is not happening in Chrome. 看起来在Firefox中,对.slideToggle()的调用会触发DOMSubtreeModified事件,而Chrome中则不会发生这种情况。 So basically in Firefox, something initially triggers the event which binds your click handler. 所以基本上在Firefox中,某些东西最初会触发绑定点击处理程序的事件。 All is good at this point. 在这一点上一切都很好。 When you then proceed to click, the slideToggle happens as expected. 然后,当您继续单击时, slideToggle按预期发生。 However, that fires off the DOMSubtreeModified event and you then end up with two click event handlers both doing slideToggle because they were now registered twice. 然而,这会触发DOMSubtreeModified事件,然后你最终会有两个click事件处理程序都执行slideToggle因为它们现在已经注册了两次。 The next time you click is when the infinite loop happens. 下次单击时是无限循环发生的时间。 Basically the multiple click events keep triggering DOMSubtreeModified which registers more click handlers which makes more slideToggles happen which triggers more DOMSubtreeModified s, and so on and so forth. 基本上,多次单击事件会一直触发DOMSubtreeModified ,它会注册更多的点击处理程序,这会使更多的slideToggles发生,从而触发更多的DOMSubtreeModified ,依此类推。 To fix this, you can use jQuery's .one which tells your page to only fire off that DOMSubtreeModified handler once, which prevents this loop. 要解决这个问题,你可以使用jQuery的.one ,告诉你的页面只触发一次DOMSubtreeModified处理程序,这会阻止这个循环。 If that is not a suitable solution, you'll just need to come up with some other way to make sure the .click handlers do not get bound more than once. 如果这不是一个合适的解决方案,你只需要提出一些其他的方法来确保.click处理程序不会被绑定多次。

jQuery('#term').one("DOMSubtreeModified",function(){   //Notice this is using .one and not .on

Check out this JSFiddle - it is using .one but I was able to verify that when using .on, the problem happened in Firefox and not Chrome. 看看这个JSFiddle - 它正在使用.one但是我能够验证在使用.on时,问题发生在Firefox而不是Chrome。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM