<iframe class="class_name">
<html>
<head></head>
<body>
<div>
<!-- All of the stuff -->
</div>
</body>
</html>
</iframe>
如何访问此 iframe 的元素?
.frame("class='classname'")
上述方法无效。
最佳答案
虽然两个 iframe 具有相同的类,但这只会隐藏第一个 iframe。
function hide() {
var iframe = document.getElementsByClassName("iframe");
iframe[0].style.display = "none";
}
<iframe class="iframe"></iframe>
<iframe class="iframe"></iframe>
<br>
<button onclick="hide()">Hide 1st Iframe</button>
这段代码有什么作用?
document.getElementsByClassName("iframe")
获取类别为 iframe
的所有元素并将元素设置为名为 iframe
的变量的值.里面的数字[]
定义众多元素中的一个。如果里面的数字[]
是0
,元素组中的第一个元素被定义。在[]
之后, 你可以设置你想要的属性。
关于javascript - 使用类名访问 iframe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49506830/