我需要一个按钮来更改它的URL以匹配屏幕上显示的iframe,我在这里面临的问题是如何用JavaScript更改按钮的URL?
以下是我现在所拥有的:
<button id="t7"onclick="t7()">Full Pilot</button><br>
function f0() {document.getElementById('i').src="https://pilot.wright.edu/d2l/home";}
function f1() {document.getElementById('i').src="https://pilot.wright.edu/d2l/home/159483";}
function f2() {document.getElementById('i').src="https://pilot.wright.edu/d2l/le/content/219408/Home";}
function f3() {document.getElementById('i').src="https://pilot.wright.edu/d2l/home/159465";}
function f4() {document.getElementById('i').src="https://pilot.wright.edu/d2l/home/219301";}
function f5() {document.getElementById('i').src="https://pilot.wright.edu/d2l/home/159463";}函数f0 - f5是我需要更改第一行代码中列出的按钮的网址的按钮。任何一个人都能指出我可以做这件事的方向,甚至是一些关于我如何去做这件事的想法。
以下是我尝试过的一件事:
function t7() {window.open("document.getElementById("i").src="");}发布于 2014-03-19 02:14:02
首先,按钮应该具有“类型”属性,不同的浏览器对于按钮元素可能有不同的默认值。
其次,按钮没有src属性,所以您不能更改按钮的src,而是尝试使用<a>。
第三,iframe支持onload事件,它将在iframe加载后触发。
所以这可能就是你想要的
<iframe src="http://www.mysite.com" onload="iload(this)"></iframe>
<a href="#" id="t7">Full Pivot</a>
<script>
function iload(obj) {
document.getElementById("t7").href = obj.src;
}
</script>更新:无href方法
<iframe src="http://www.mysite.com" onload="iload(this)"></iframe>
<button type="button" id="t7">Full Pivot</a>
<script>
function iload(obj) {
document.getElementById("t7").onclick = function() {
window.location.href=obj.src;
}
}
</script>或者,采取一种较短的办法。
<iframe src="http://www.mysite.com" id="the_iframe"></iframe>
<button type="button" onclick="iload">Full Pivot</a>
<script>
function iload() {
window.location.href=document.getElementById("the_iframe").src;
}
</script>发布于 2014-03-19 01:40:28
尝试document.getElementById('t7').onclick="...";更改“url”。
发布于 2014-03-25 20:08:29
JavaScript:第一个按钮按:
function f0(){document.getElementById('i').src="https://pilot.wright.edu/d2l/home";document.getElementById('o').innerHTML="https://pilot.wright.edu/d2l/home"}HTML:
<p id="o">https://pilot.wright.edu/d2l/home</p>解决办法:
function t7() {window.open(document.getElementById('o').innerHTML);}基本上,我只是添加了一个段落节点,并共享到按钮的innerHTML的URL
感谢所有回答的人
https://stackoverflow.com/questions/22494548
复制相似问题