简体   繁体   English

动态子内容上的 MutationObserver

[英]MutationObserver on dynamic child content

I'm working on a Prestashop site project with a particularly fiddly megamenu setup.我正在开发一个 Prestashop 站点项目,该项目具有特别繁琐的 megamenu 设置。

The MutationObserver does a pretty good job of pushing the menu back when the cart overlay is open, but the issue if the menu is dynamically pulled in by another JavaScript library that is obfusticated beyond any rescue and as such the MutationObserver gets unlinked from the node each time, causing everything to be drawn incorrectly if anything is added to the cart after page load.当购物车覆盖打开时, MutationObserver在推回菜单方面做得很好,但是如果菜单被另一个 JavaScript 库动态拉入,这个问题在任何救援之外都被混淆了,因此MutationObserver每个都与节点断开链接时间,如果在页面加载后将任何内容添加到购物车,则会导致所有内容都被错误地绘制。

The MutationObserver is currently linked directly to the main page container object ( category ). MutationObserver当前直接链接到主页容器 object( category )。

//  CREATE A MUTATION OBSERVER OBJECT TO MONITOR ANY STATE CHANGES ON THE JS NODE
    let observer = new MutationObserver(blockCartOpened);

    function blockCartOpened(mutationRecords) {
    //  PROCESS THE LIST OF MUTATION THAT HAVE OCCURED
        mutationRecords.forEach (function (mutation) {
        //  GET THE MUTATION TARGET AND SEE IF THE DISPLAY STYLE ATTRIBUTE IS SET OR NOT
        //  AND REACT ACCORDINGLY
            var target = $(mutation.target);
            if(target.hasClass("cart_block")) {
                if(target.css("display") == "block") {
                    $(".ets_mm_megamenu").css("z-index", "-1");
                } else {
                    $(".ets_mm_megamenu").css("z-index", "2");
                }
            }
            mutation.addedNodes.forEach(function(node) {
            //  Nothing to be here yet...
            });
        });
    }

//  SET THE TARGETNODE AS WELL AS THE SETTINGS FOR WHAT TO TRIGGER THE OBSERVER ON, IN THIS CASE
//  TRIGGER THE OBSERVER ON ANY CHANGES TO THE STYLE ATTRIBURES OF THE NODE
    var targetNodes = document.getElementById("category");
    var observerOptions = {
        childList: true,
        subtree: true,
        attributes: true,
        attributeFilter: ["style"]
    }

//  SINCE THIS IS A CLASS, A EACH LOOP IS REQUIRED EVEN IF THERE IS ONLY ONE RESULT
    observer.observe(targetNodes, observerOptions);

Is there a way to monitor for the attribute changes on created child nodes inside the parent, or is this just a pipe dream?有没有办法监控父节点内创建的子节点的属性变化,或者这只是 pipe 的梦想?

Well in the end I didn't have to use a MutationObserver, since the issue proved to be unsuited for that sort of solution.好吧,最后我不必使用 MutationObserver,因为事实证明这个问题不适合那种解决方案。 So I took the path of least resistence, ie the solution isn't elegant but it works PERFECTLY:所以我选择了阻力最小的路径,即解决方案并不优雅,但效果很好:

//  BPK: CODE ADDITIONS
    $(document).on("click", function(event) {
        var target = $(".blockcart-header");
        if(target.hasClass("open")) {
            $(".ets_mm_megamenu").css("z-index", "-1");
        } else {
            $(".ets_mm_megamenu").css("z-index", "2");
        }
    });

    $(document).on("click", ".blockcart-header a", function(event) {
        var target = $(".blockcart-header");
        if(target.hasClass("open")) {
            $(".ets_mm_megamenu").css("z-index", "-1");
        } else {
            $(".ets_mm_megamenu").css("z-index", "2");
        }
    });
//  END BPK

Using a document wide click event to monitor for "closing" clicks and a click event on the trigger button for the cart display just to be sure solved the problem for me.使用文档范围的点击事件来监控“关闭”点击和购物车显示的触发按钮上的点击事件,以确保为我解决了问题。

Lesson: Don't try to go too complicated to solve a problem...教训:不要试图把 go 太复杂来解决问题...

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

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