gpt4 book ai didi

javascript - 使用类名访问 iframe

转载 作者:行者123 更新时间:2023-11-28 00:47:46 24 4
gpt4 key购买 nike

<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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com