检查嵌入标签是否存在 javascript
如何检查我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在附加之前添加
soundEmbed.id = "__soundEmbed";
。然后,用以下内容包围整个事物:Add
soundEmbed.id = "__soundEmbed";
before appending. Then, surround the entire thing with:您可以给元素一个 id 并首先检查它:
或者,检查一个标志(因为 soundEmbed 似乎在其他地方定义,我们可以对其使用错误检查):
You could give the element an id and check this first:
Or alternatively, check against a flag (since soundEmbed seems to be defined elsewhere we can use a falsey check on it):
它不会在您的 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 totrue
when you create theembed
element.