[英]MutationObserver: new value in the format of `oldValue`
我正在使用MutationObserver
並且我想訪問屬性的新值。 我試試這個:
newValue = mutation.target[mutation.attributeName]
但是,如果例如mutation.attributeName == 'style'
,則mutation.oldValue
和 my newValue
的格式不同: mutation.oldValue
是一個格式類似於newValue.cssText
的字符串,而newValue
本身是一個CSSStyleDeclaration
object。
我想避免為每個可能的mutation.attributeName
編程,例如“如果'style'
,使用.cssText
,如果......,使用......”。 可能的屬性太多了。
如何獲取oldValue
格式的新值?
相關問題不回答我的問題:
編輯:
屬性和屬性是不同的東西。 要獲取屬性字符串,您可以使用getAttribute
:
newValue = mutation.target.getAttribute(mutation.attributeName);
您正在從元素實例訪問屬性,在style
的情況下,它是一個CSSStyleDeclaration
object 反映來自style
屬性的已解析樣式屬性。 雖然許多屬性直接反映為屬性( id
、 name
),但其他屬性則采用不同的形式(如style
)或根本不能用作元素的屬性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.