用JavaScript改变JavaScript

时间:2014-03-19 01:34:35

标签: javascript html

我需要一个按钮来更改它的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是我需要更改第一行代码中列出的按钮的URL的按钮。任何人都可以指出我可以做到这一点的方向,甚至可以指出一些关于如何做到这一点的想法。

这是我尝试过的一件事:

    function t7() {window.open("document.getElementById("i").src="");}

3 个答案:

答案 0 :(得分:0)

尝试document.getElementById('t7').onclick="...";更改“网址”。

答案 1 :(得分:0)

首先,按钮应具有“类型”属性,不同的浏览器可能具有不同的按钮元素默认值。

第二,按钮没有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>

答案 2 :(得分:0)

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);}

我基本上只是添加了一个段落节点并将该URL共享到按钮的innerHTML

感谢所有回答

的人