【发布时间】:2013-05-04 22:25:08
【问题描述】:
我正在处理一个画廊页面,其中包含很多使用 iframe 嵌入的外部 HTML 页面,但是,其中一些外部页面中嵌入了音频,我想将其静音。
这是 HTML 文件的示例代码
gallery.html
<div id="pages">
<iframe id="embed" src="https://xyz.com/page1.html"></iframe>
<iframe id="embed" src="https://sdf.com/page2.html"></iframe>
</div>
page1.html
<html>
<head>
<!-- head stuff-->
</head>
<body>
<audio autoplay>
<source src="http://audio.url/somefile.ogg" type="audio/ogg">
<source src="http://audio.url/somefile.mp3" type="audio/mpeg">
Your browser does not support the audio tag.
</audio>
<!--some more body content>
<body>
</html>
此外,由于我正在处理多个外部页面,因此音频标签大多可能没有附加 ID。这使得document.getElementById() 成为一个不可靠的选项。我已经尝试过document.getElementsByTagName("input"),但无法完全静音。谁能帮我解决这个问题?
【问题讨论】:
-
我也试过:
document.getElementsByTagName("audio").muted = true;但这个方法似乎也失败了。 -
为什么不在音频上设置控件,因为如果没有 id,尝试使用 vanilla JS 在 iframe 的另一侧定位元素是很困难的。
标签: javascript html audio mute