我正在尝试创建一个iframe,它在单击按钮时会发生更改
I am trying to create an iframe that changes when a button is clicked
我正在建立一个网站,我需要的是当点击一个按钮时,它会更改iframe中的链接,从而转到另一个网站。到目前为止,我得到的是:
<script>
var id = 'http://www.aWebsite.com';
function link(){
id = 'http://www.aSecondWebsite.com'
}
</script>
<script>
document.writeln('<iframe name="heel"src="' + id + '" height="300" width="700"> </iframe>');
</script>
<button onclick="link()">Click Me</button>
我不知道为什么当点击按钮时id变量没有改变?
实际上id
的值发生了更改,但document.writeln()
不在link
函数中,因此不执行。
一个快速的解决方案是将document.writeln()
移动到link()
,但这将是一个灾难性的解决方案。document.write(ln)()
清除文档中的所有内容,并在解析页面后使用时创建一个新文档。你需要做这样的事情:
function link() {
id = 'http://www.example.org';
document.getElementById('heel').src = id;
}
然后将iframe元素添加到HTML中,并使用document.writeln()
删除整个脚本。
<iframe id="heel" name="heel" src="http://www.example.com" height="300" width="700"></iframe>
编辑
如果你想创建一个新的iframe
元素,你可以这样做:
function link() {
var frame = document.createElement('iframe'); // creates an iframe element
id = 'http://www.example.org';
frame.width = '700px'; // sets the width for the new iframe
frame.height = '300px'; // sets the height for the new iframe
frame.name = 'heel2'; // sets the name for the new iframe
frame.src = id; // sets the src for the new iframe
document.body.appendChild(frame); // appends the new iframe to body as a last element
}
相关文章:
- 如何隐藏按钮单击事件上的占位符
- 从Web服务器下载图像按钮单击使用JavaScript
- 在SweetAlert中传递ASP.NET按钮单击事件
- 单选按钮单击可刷新/更改网站的小区域
- 调用按钮单击事件 ajax 加载的用户控件
- 附加 jQuery 代码以在加载和按钮单击时运行
- 在按钮单击时将图像URL数据显示到弹出框中,而无需禁用背景
- 按钮单击服务器单击
- 触发单选按钮单击,并在页面刷新时记住选择
- 如何使用 javascript 而不是使用控制器中的方法在 rails 中呈现部分按钮单击
- jQuery UI 选项卡 - 将参数设置为下一个/上一个按钮单击
- 如何在 Angular JS 中的按钮单击上添加类
- 当元素上有多个类时触发按钮单击事件
- 如何在 HTML 中的按钮单击上更改图像(IMG URL 每次来自服务器)
- 如何使用javascript刷新我的html页面时清除按钮单击
- 阻止后退按钮 - 单击按钮时忽略
- 如何制作按钮.单击“开始时不运行”
- Rails:尝试在按钮单击时渲染部分
- 在页面加载时调用函数,在按钮单击时再次调用函数,但参数不同
- 如何在服务器端的文本中添加按钮单击事件