在Javascript中,有没有一种技术可以监听title元素的更改?
最佳答案
5年后,我们终于有了更好的解决方案。使用MutationObserver!
简而言之:
new MutationObserver(function(mutations) {
console.log(mutations[0].target.nodeValue);
}).observe(
document.querySelector('title'),
{ subtree: true, characterData: true, childList: true }
);
有评论:// select the target node
var target = document.querySelector('title');
// create an observer instance
var observer = new MutationObserver(function(mutations) {
// We need only first event and only new value of the title
console.log(mutations[0].target.nodeValue);
});
// configuration of the observer:
var config = { subtree: true, characterData: true, childList: true };
// pass in the target node, as well as the observer options
observer.observe(target, config);
也是Mutation Observer has awesome browser support:关于javascript - 如何收听对title元素的更改?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2497200/