2012-08-30 43 views
6

Bất kỳ triển khai được đề xuất nào của trình nghe thay đổi thuộc tính CSS? Có thể:Trình nghe thay đổi thuộc tính 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)} 
    } 
    } 
} 

Trả lời

3

Tôi nghĩ rằng bạn đang tìm kiếm này:

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

Nếu bạn google cho nó, một loạt các công cụ đi lên. Điều này có vẻ đầy hứa hẹn mặc dù:

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

+0

Cái này không hoạt động trên các trình duyệt WebKit dựa, Chrome ít nhất. –

2

sự kiện đột biến như DOMAttrModified đang bị phản đối. Hãy xem xét sử dụng một MutationObserver thay thế.

Ví dụ:

<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); 
Các vấn đề liên quan