[英]jQuery: Listener to input attribute change
<textarea id="test" disabled>Test</textarea>
有沒有辦法監聽屬性的變化? 比如disabled
屬性。
我需要一個事件來添加或刪除“禁用”屬性。 像這樣的東西:
$("#test").on("change attribute",function(){
alert("Textarea enabled");
})
您可以使用 MutationObserver:
console.log('script start'); const test = document.querySelector('#test'); new MutationObserver((mutations) => { for (const mutation of mutations) { if (mutation.attributeName === 'disabled') { console.log('Change detected'); } } }).observe(test, { attributes: true }); setTimeout(() => { test.removeAttribute('disabled'); }, 1000);
<textarea id="test" disabled>Test</textarea>
不需要像 jQuery 這樣的外部庫; 現在基本上所有的瀏覽器都支持這一點。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.