2012-08-30 98 views
6

任何建議的CSS屬性更改監聽器實現?也許:CSS屬性更改監聽器

thread = 

function getValues(){ 
    while(true){ 
    for each CSS property{ 
     if(properties[property] != nil && getValue(property) != properties[property]){alert('change')} 
     else{properties[property] = getValue(property)} 
    } 
    } 
} 

回答

3

我認爲你正在尋找這樣的:

document.documentElement.addEventListener('DOMAttrModified', function(e){ 
    if (e.attrName === 'style') { 
    console.log('prevValue: ' + e.prevValue, 'newValue: ' + e.newValue); 
    } 
}, false); 

如果谷歌爲它,一堆東西上來。這看起來雖然有前途:

http://darcyclarke.me/development/detect-attribute-changes-with-jquery/

+0

這一個不上基於WebKit瀏覽器,瀏覽器至少。 –

2

DOMAttrModified突變事件已被棄用。考慮改用MutationObserver。

實施例:

<div>use devtools to change the <code>background-color</code> property of this node to <code>red</code></div> 
<p>status...</p> 

JS:

var observer = new MutationObserver((mutations) => { 
    mutations.forEach((mutation) => { 
    if (mutation.target.style.color === 'red') { 
     document.querySelector('p').textContent = 'success'; 
    } 
    }); 
}); 

var observerConfig = { 
    attributes: true, 
    childList: false, 
    characterData: false, 
    attributeOldValue: true 
}; 

var targetNode = document.querySelector('div'); 
observer.observe(targetNode, observerConfig);