Advanced CSS Selector - Select based on styling

前端 未结 4 1647
别那么骄傲
别那么骄傲 2020-12-20 23:05

Performance issues aside, is it possible to use a style as a selector? For example, something like:

div[background-image=\"img/someimg.jpg\"] {opacity:.5}
         


        
4条回答
  •  青春惊慌失措
    2020-12-20 23:22

    There's something called DOMSubtreeModified which has now been turned into MutationObserver. This can help you watch the dom for when new elements are added:

    // identify an element to observe
    var elementToObserve = document.querySelector("#targetElementId");
    
    // create a new instance of `MutationObserver` named `observer`, 
    // passing it a callback function
    var observer = new MutationObserver(function() {
        console.log('callback that runs when observer is triggered');
    });
    
    // call `observe` on that MutationObserver instance, 
    // passing it the element to observe, and the options object
    observer.observe(elementToObserve, {subtree: true, childList: true});
    

    This example is copy/pasted from mdn docs: https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver/observe

提交回复
热议问题