如果这个问题之前已经被回答了,我很抱歉,我在搜索中找不到。
当4个元素取消隐藏(“display:none”被删除)时,我必须标记它们以触发事件。我认为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
移除的任何脚本/行为中触发自定义事件? - David Thomas