我正在学习使用Mutation观察器类,主要是在这里:https://javascript.info/mutation-observer
我知道如何聆听属性的更改,以及如何使用attributeOldValue获取旧的属性值。但是我可以从同一事件中获取新的属性值吗?如果是这样,怎么办?谢谢
答案 0 :(得分:2)
您可以直接从更改的对象中直接查询当前值。
// is element is an input
const newValue = document.getElementByID('myElement').value;
// or, if it is a any text element
const newValue = document.getElementByID('myElement').innerHTML;
此外,每个MutationRecord
都有一个名为target
的属性,其中包含有关已更改对象的所有详细信息。因此,根据MutationRecord.type
(它告诉我们发生了什么类型的更改),您可以从MutationRecord.target
内的相应属性中获取新值