如何使用iframe中的javascript按标记名称更改元素

时间:2017-09-15 12:30:55

标签: javascript styles getelementsbytagname getelementsbyname

我正在尝试从样式中更改url(herf)。检查

<iframe src="https://www.website.com/............" heght="100%" width="100%" >

#document

<!DOCTYPE html>

<html> 
<head></head>
<body>

<a class="class"  href="https://www.website.com/-----"  style="background: url(&quot;https://www.website.com/abc.png&quot;) center center / 100% no-repeat; width: 110px; height: 26px;"></a>

</body>
</html>

<iframe>

我想用javascript更改图片网址

: style="background: url(&quot;https://www.website.com/abc.png&quot;)

使用另一个网址。

喜欢那个

: style="background: url(&quot;https://www.website.com/xyz.png&quot;)

图片xyz.png非常重要。

4 个答案:

答案 0 :(得分:0)

document.getElementsByTagName('iframe')[0].src = "https://www.website.com/xyz.png";

获取iframe src并更新src。 希望这会对你有所帮助

答案 1 :(得分:0)

要从Javascript更改元素的样式,您可以这样做:

<!-- Suppose you have this <a> element -->
<a id="my-link" style="background: ...; ...">...</a>

<!-- This is the code -->
<script>
    document.getElementById("my-link").style.background = "url('https://www.website.com/abc.png') center center / 100% no-repeat";
</script>

这是你想要的吗?

答案 2 :(得分:0)

<script>
document.getElementById("iframe").setAttribute('style',' background-image: url(https://www.website.com/xyz.png);background-repeat: no-repeat;    background-size: 100% 100%;');
</script>

试试这个

答案 3 :(得分:0)

我找到它,试一试

<!DOCTYPE html>
<html>
<body>

<ul>
  <li>Coffee</li>
  <li>Tea</li>
</ul>

<p>Click the button to change the text of the first list item (index 0).</p>

<button onclick="myFunction()">Try it</button>

<script>
function myFunction() {
    var list = document.getElementsByTagName("UL")[0];
    list.getElementsByTagName("LI")[0].innerHTML = "Milk";
}
</script>

</body>
</html>