检查嵌入标签是否存在 javascript

发布于 2025-01-04 05:18:01 字数 457 浏览 1 评论 0原文

如何检查我的 html 中是否存在嵌入标签。 我已经通过 javascript 创建了这个标签

    soundEmbed = document.createElement("embed");
    soundEmbed.setAttribute("src", "notify.wav");
    soundEmbed.setAttribute("hidden", true);
    soundEmbed.setAttribute("autostart", true);
    soundEmbed.setAttribute("loop", false);
    document.body.appendChild(soundEmbed);

,但由于我使用自动刷新和appendChild,每次刷新时都会产生额外的声音。

使用appendChild,我想我有很多嵌入标签。我认为这可能是额外声音的原因。

How can I check if embed tag exist in my html.
I've created this tag through javascript

    soundEmbed = document.createElement("embed");
    soundEmbed.setAttribute("src", "notify.wav");
    soundEmbed.setAttribute("hidden", true);
    soundEmbed.setAttribute("autostart", true);
    soundEmbed.setAttribute("loop", false);
    document.body.appendChild(soundEmbed);

but since I am using autorefresh and appendChild, every time it refresh it creates additional sound.

Using appendChild, I think I've got many embed tags. That's what I think maybe the cause of additional sound.

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

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

发布评论

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

评论(3

昔梦 2025-01-11 05:18:01

在附加之前添加 soundEmbed.id = "__soundEmbed";。然后,用以下内容包围整个事物:

if( !document.getElementById("__soundEmbed")) {
    soundEmbed = ......;
    ......
}

Add soundEmbed.id = "__soundEmbed"; before appending. Then, surround the entire thing with:

if( !document.getElementById("__soundEmbed")) {
    soundEmbed = ......;
    ......
}
花开浅夏 2025-01-11 05:18:01

您可以给元素一个 id 并首先检查它:

if (!document.getElementById('someId')) {
  var soundEmbed = document.createElement("embed");
  soundEmbed.setAttribute("src", "notify.wav");
  soundEmbed.setAttribute("hidden", true);
  soundEmbed.setAttribute("autostart", true);
  soundEmbed.setAttribute("loop", false);
  soundEmbed.setAttribute("id", 'someId');
  document.body.appendChild(soundEmbed);
}

或者,检查一个标志(因为 soundEmbed 似乎在其他地方定义,我们可以对其使用错误检查):

if (!soundEmbed) {
  soundEmbed = document.createElement("embed");
  soundEmbed.setAttribute("src", "notify.wav");
  soundEmbed.setAttribute("hidden", true);
  soundEmbed.setAttribute("autostart", true);
  soundEmbed.setAttribute("loop", false);
  document.body.appendChild(soundEmbed);
}

You could give the element an id and check this first:

if (!document.getElementById('someId')) {
  var soundEmbed = document.createElement("embed");
  soundEmbed.setAttribute("src", "notify.wav");
  soundEmbed.setAttribute("hidden", true);
  soundEmbed.setAttribute("autostart", true);
  soundEmbed.setAttribute("loop", false);
  soundEmbed.setAttribute("id", 'someId');
  document.body.appendChild(soundEmbed);
}

Or alternatively, check against a flag (since soundEmbed seems to be defined elsewhere we can use a falsey check on it):

if (!soundEmbed) {
  soundEmbed = document.createElement("embed");
  soundEmbed.setAttribute("src", "notify.wav");
  soundEmbed.setAttribute("hidden", true);
  soundEmbed.setAttribute("autostart", true);
  soundEmbed.setAttribute("loop", false);
  document.body.appendChild(soundEmbed);
}
鯉魚旗 2025-01-11 05:18:01

它不会在您的 HTML 中,但会在 DOM 树中(您在解析 HTML 后将其添加到 DOM,因此它永远看不到任何 HTML)。一个超级简单的解决方案是只维护一个全局变量,告诉您它是否已创建 - 只需将其初始化为 false,并在您创建时将其设置为 true创建 embed 元素。

It won't be in your HTML, but it will be in the DOM tree (you're adding it to the DOM after the HTML has been parsed, so it never sees any HTML). A super-simple solution is to just maintain a global variable that tells you whether it's been created yet or not - just initialize it to false, and set it to true when you create the embed element.

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