使用 javascript 在 div 中获取 iframe
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8683145/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
get iframe inside div with javascript
提问by Rod
Is there a way to do the following in JavaScript?
有没有办法在 JavaScript 中执行以下操作?
- get a div by a class name,
- get the iframe inside the div
- and add an attribute (frameborder = 0)
- 通过类名获取 div,
- 获取 div 内的 iframe
- 并添加一个属性(frameborder = 0)
<html>
<body>
<div class="calendar">
<iframe></iframe>
</div>
</body>
</html>
回答by Adam Rackis
To get a div by class name:
要按类名获取 div:
var allDivs = document.getElementsByClassName("calendar");
Assuming there's only one
假设只有一个
var calendarDiv = document.getElementsByClassName("calendar")[0];
Get the iFrame inside:
获取里面的 iFrame:
var iFrame = calendarDiv.getElementsByTagName("iframe")[0];
set an attribute:
设置一个属性:
iFrame.setAttribute("frameborder", "0");
Or, since getElementsByClassName
isn't supported in old versions of IE, consider setting the id on the iFrame
and simply doing:
或者,由于getElementsByClassName
旧版本的 IE 不支持,请考虑在 上设置 idiFrame
并简单地执行以下操作:
document.getElementById("iFrameId").setAttribute("frameborder", "0");
回答by matan7890
First of all, you need to give your iframe id.
首先,您需要提供您的 iframe id。
After that, you can get your iframe with the function document.getElementById(""), and than to change it's property.
之后,您可以使用函数 document.getElementById("") 获取 iframe,而不是更改它的属性。
For example:
例如:
<html>
<body>
<div class="calendar">
<iframe id="myIframe"></iframe>
</div>
<script type="text/javascript">
document.getElementById("myIframe").frameborder = 1;
</script>
</body>
</html>