我试图使用突变处理器检测特定元素的变化,但它不起作用。我在做什么错?

发布于 2025-01-31 22:25:50 字数 631 浏览 2 评论 0原文

我一直在尝试从HTML文档记录更新的成本信息,但还没有成功。我确信必须是我无法正确使用突变观察者。 JS文件已链接。以下是我针对的HTML元素。

html:

< span ID =“ cost”> 0.00</span>

js文件包含以下代码:


const total = document.getElementById('cost');

console.log(total);
const options = {
  characterData: true
};

function logCallback(mutations) {
  console.log('called');

  for (let mutation of mutations) {
    if (mutation.type === 'characterData')
    {
      console.log('Mutation Detected: Price changed');
    }
  }
}

const observer = new MutationObserver(logCallback);

observer.observe(total.childNodes[0], options);

I have been trying to log the updated cost information from an HTML document but have not been successful. I am sure it has to be that I am not using the mutation observer correctly. The js file is linked. Below is the HTML element that I am targeting.

HTML:

<span id="cost">0.00</span>

The JS file contains the following code:


const total = document.getElementById('cost');

console.log(total);
const options = {
  characterData: true
};

function logCallback(mutations) {
  console.log('called');

  for (let mutation of mutations) {
    if (mutation.type === 'characterData')
    {
      console.log('Mutation Detected: Price changed');
    }
  }
}

const observer = new MutationObserver(logCallback);

observer.observe(total.childNodes[0], options);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

农村范ル 2025-02-07 22:25:50

如果 使更改的代码更改了您正在观看的文本节点,而不是破坏和替换它,则您的代码将起作用,例如:

total.childNodes[0].nodeValue = "1.00";

实时示例:

const total = document.getElementById('cost');

const options = {
  characterData: true,
};

function logCallback(mutations) {
  console.log('called');

  for (let mutation of mutations) {
    if (mutation.type === 'characterData')
    {
      console.log('Mutation Detected: Price changed');
    }
  }
}

const observer = new MutationObserver(logCallback);

observer.observe(total.childNodes[0], options);

// Code that changes the element
total.childNodes[0].nodeValue = "1.00";
<span id="cost">0.00</span>

但是进行更改的代码可能正在破坏和替换子节点,因此:

total.innerText = "1.00";

要处理,您最好观看child> child -list更改total element上的更改本身:

const total = document.getElementById('cost');

const options = {
  childList: true,
};

function logCallback(mutations) {
  console.log('called');

  for (let mutation of mutations) {
    console.log('Mutation Detected: Price changed');
  }
}

const observer = new MutationObserver(logCallback);

observer.observe(total, options);

// Code that changes the element
total.innerText = "1.00";
<span id="cost">0.00</span>

您可能需要完善该(如果仅检查targindata更改)和/或将其与您已经拥有的内容相结合(以防万一其他代码更改文字不同),但这是您所拥有的主要问题。

Your code would work if the code that was making the change changed the text node you're watching rather than destroying and replacing it, like this:

total.childNodes[0].nodeValue = "1.00";

Live Example:

const total = document.getElementById('cost');

const options = {
  characterData: true,
};

function logCallback(mutations) {
  console.log('called');

  for (let mutation of mutations) {
    if (mutation.type === 'characterData')
    {
      console.log('Mutation Detected: Price changed');
    }
  }
}

const observer = new MutationObserver(logCallback);

observer.observe(total.childNodes[0], options);

// Code that changes the element
total.childNodes[0].nodeValue = "1.00";
<span id="cost">0.00</span>

But the code making the change is probably destroying and replacing the child node, like this:

total.innerText = "1.00";

To handle that, you're probably best off watching for childList changes on the total element itself:

const total = document.getElementById('cost');

const options = {
  childList: true,
};

function logCallback(mutations) {
  console.log('called');

  for (let mutation of mutations) {
    console.log('Mutation Detected: Price changed');
  }
}

const observer = new MutationObserver(logCallback);

observer.observe(total, options);

// Code that changes the element
total.innerText = "1.00";
<span id="cost">0.00</span>

You may need to refine that (I removed the if checking only for characterData changes), and/or combine it with what you already had (in case some other code changes the text differently), but that's the main issue with what you had.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文