点击按钮button触发修改div的iframe内容 (修改iframe的src内容)

新建一个html文件,比如test.html,之后将如下内容复制进文件,然后用浏览器打开该html文件即可

<html>
<script src="http://apps.bdimg.com/libs/jquery/1.6.4/jquery.min.js"></script>
<head>
<meta charset="utf-8">
<title>div中动态嵌入iframe内容</title>
</head>
<body>
<table style="height: 100%;width: 100%">
    <tr>
        <td>
            <button type="button" onclick="click1()">button1</button>
            <br>
            <button type="button" onclick="click2()">button2</button>
            <br>
            <button type="button" onclick="click3()">button3</button>
        </td>
        <td>
            <div id="desdiv" style="width: 1000px;height: 400px;">
                <span>初始化</span>
            </div>
        </td>
    </tr>
</table>
<script>
function click1() {
alert("click1");
document.getElementById('desdiv').innerHTML = "<span>变更后</span>";
}
function click2() {
alert("click2");
document.getElementById('desdiv').innerHTML = "<iframe src='http://www.baidu.com'"+"style='width: 100%;height: 100%;'"+"></iframe>";
}
function click3() {
alert("click3");
document.getElementById('desdiv').innerHTML = "<iframe src='http://www.sina.com'"+"style='width: 1000px;height: 400px;'"+"></iframe>";
}
</script>
</body>
</html>

jsp文件同理

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
修改Vueiframe的背景颜色,你可以采取以下步骤: 1. 首先,在Vue组件中找到包含iframe的标签或元素,并为其添加一个唯一的id,方便后续操作。例如,你可以给iframe所在的div元素添加一个id="my-iframe"。 2. 接下来,在Vue组件的methods选项中编写一个方法,用于修改iframe的背景颜色。你可以称此方法为changeIframeBgColor()。 3. 在这个方法中,你可以通过获取到iframe的DOM元素,使用Vue的$refs属性来实现。在changeIframeBgColor()方法中添加以下代码: ```javascript changeIframeBgColor() { const iframeElement = this.$refs.myIframe.contentDocument.body; iframeElement.style.backgroundColor = 'red'; } ``` 这段代码中,我们通过this.$refs.myIframe获取到了id为"my-iframe"的元素,然后使用contentDocument获取到了iframe的文档对象。最后,我们将iframe的背景颜色设置为红色。 4. 最后,在Vue组件的模板中,将changeIframeBgColor()方法绑定到一个按钮或其他事件上,以触发修改背景颜色的操作。例如: ```html <div id="my-iframe"> <iframe ref="myIframe" src="http://example.com"></iframe> <button @click="changeIframeBgColor">修改背景颜色</button> </div> ``` 在这个例子中,我们在按钮上添加了点击事件,点击按钮就会触发changeIframeBgColor()方法,从而修改iframe的背景颜色为红色。 请注意,为了获得正确的元素引用,需要确保iframe的加载是同步的,并且iframeVue组件在同一个域中。如果跨域,可能会遇到安全限制。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值