簡體   English   中英

MutationObserver: `oldValue` 格式的新值

[英]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格式的新值?


相關問題不回答我的問題:

  • 在我的情況下,此處mutation["addedNodes"][0]是未定義的。
  • 此答案要求元素具有 ID。 在某些情況下,這可能很難實現。

編輯:

  • 這個答案建議newValue = mutation.target.attributes.getNamedItem(mutation.attributeName) ,但它的格式也與mutation.oldValue不同。 有關詳細信息,請參閱此處

屬性和屬性是不同的東西。 要獲取屬性字符串,您可以使用getAttribute

newValue = mutation.target.getAttribute(mutation.attributeName);

您正在從元素實例訪問屬性,在style的情況下,它是一個CSSStyleDeclaration object 反映來自style屬性的已解析樣式屬性。 雖然許多屬性直接反映為屬性( idname ),但其他屬性則采用不同的形式(如style )或根本不能用作元素的屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM