Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript/jQuery:当;显示:无“;被移除了吗?_Javascript_Jquery_Mutation Observers - Fatal编程技术网

JavaScript/jQuery:当;显示:无“;被移除了吗?

JavaScript/jQuery:当;显示:无“;被移除了吗?,javascript,jquery,mutation-observers,Javascript,Jquery,Mutation Observers,很抱歉,如果之前已回答此问题,我无法使用搜索找到它 我必须标记4个元素,以便在取消隐藏事件时触发事件(“显示:无”被删除)。我认为使用mutationObserver是正确的选择,但是我在过滤到我需要的事件时遇到了困难 有人有这方面的经验吗 编辑:不幸的是,我们没有访问脚本的权限,实际上取消隐藏元素。它们由另一个团队拥有和保护。我们唯一能确定的是当元素未隐藏时 我想知道如何根据我的需要编辑这个 // Blocker is the element that has a changing d

很抱歉,如果之前已回答此问题,我无法使用搜索找到它

我必须标记4个元素,以便在取消隐藏事件时触发事件(“显示:无”被删除)。我认为使用mutationObserver是正确的选择,但是我在过滤到我需要的事件时遇到了困难

有人有这方面的经验吗

编辑:不幸的是,我们没有访问脚本的权限,实际上取消隐藏元素。它们由另一个团队拥有和保护。我们唯一能确定的是当元素未隐藏时

我想知道如何根据我的需要编辑这个

    // Blocker is the element that has a changing display value
var blocker  = document.querySelector( '#blocker' );
// Trigger changes the display value of blocker
var trigger  = document.querySelector( '#trigger' );
// Our mutation observer, which we attach to blocker later
var observer = new MutationObserver( function( mutations ){
    mutations.forEach( function( mutation ){
        // Was it the style attribute that changed? (Maybe a classname or other attribute change could do this too? You might want to remove the attribute condition) Is display set to 'none'?
        if( mutation.attributeName === 'style' && window.getComputedStyle( blocker ).getPropertyValue( 'display' ) !== 'none'
          ){
            alert( '#blocker\'s style just changed, and its display value is no longer \'none\'' );
        }
    } );
} );

// Attach the mutation observer to blocker, and only when attribute values change
observer.observe( blocker, { attributes: true } );

// Make trigger change the style attribute of blocker
trigger.addEventListener( 'click', function(){
    blocker.removeAttribute( 'style' );
}, false );

不幸的是,我不确定您如何“删除”
display:none
,但是,如果您碰巧使用jQuery的
$.show
函数,那么您可以指定一个“回调”函数,该函数将在显示“动画”完成后调用

下面是一个例子:

$(".myElement").show(400, function() {
    // Do something...
});
同样,我不确定您是如何删除显示:无的,因此这可能没有任何帮助。

我们开始吧

var blocker  = document.querySelector('#blocker');
var previousValue = blocker.style.display;

var observer = new MutationObserver( function(mutations){
    mutations.forEach( function(mutation) {
        if (mutation.attributeName !== 'style') return;
        var currentValue = mutation.target.style.display;
        if (currentValue !== previousValue) {
           if (previousValue === "none" && currentValue !== "none") {
             console.log("display none has just been removed!");
           }

           previousValue = mutation.target.style.display;
        }
    });
});


observer.observe(blocker, { attributes: true });
基本上,我们得到了样式属性中设置的默认值(如果有)-它存储在
previousValue
中;然后我们留下代码,但我们在循环中做了一些改变。首先,我们检查目标中的样式是否已更改。然后我们在目标的样式属性中获取当前显示值。下一步是比较这些值。如果以前的值为“none”,而现在为其他值,则表示显示:none未设置。但是,您必须注意,它只侦听此特定更改。如果你不需要听“无”,那么你可以省略它


您可以在您的代码中使用它-我不是故意编写整个代码的,所以请随意在目标上添加事件侦听器。

在任何脚本/行为触发删除
显示:无
?我们无法访问该脚本。第三方标签公司。我们不是不幸的。该脚本属于另一个团队,我们无法使用。我们正在与营销人员合作,以丰富所收集的数据。请不要理会我的回答,因为它没有任何用处,对不起!我通常使用jQuery。每个选择器将类似于这些的元素应用于具有给定类的每个元素。这个答案适用于同一类的多个对象吗?如果你的意思是你想监听多个元素,那么你要做的就是迭代这些元素。您只需将该代码放在.each函数中,删除
阻塞器
变量,并在发生该变量的每个位置粘贴.each循环的第二个参数(元素)。就是这样,还值得一提的是,一旦不再需要观察者,您可以断开它的连接-只需调用observer.disconnect()