2014-03-19 89 views
0

我需要一个按钮来更改它的URL以匹配屏幕上显示的iframe,我在这里面临的问题是如何使用JavaScript更改按钮的URL?用JavaScript更改JavaScript

这是我到现在为止:

<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的按钮。任何一个人都可以指出我可以这样做的方向,甚至可以指导我如何去做这件事。

这是一两件事,我曾尝试: “URL”

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

你能告诉我们你尝试过什么到目前为止,并张贴演示重现你的具体问题? – elclanrs

+0

问题是我对如何尝试这一点甚少了解。 – CMS2

+0

那么你可能需要做更多的研究。我们期望代码在问题中寻求代码。尝试使用[MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript)获得一般JavaScript知识。当你有一些代码时,请参阅[如何写一个好问题](https://msmvps.com/blogs/jon_skeet/archive/2010/08/29/writing-the-perfect-question.aspx),你是一半在那里。 – elclanrs

回答

0

尝试document.getElementById('t7').onclick="...";改变

+0

好吧,所以我已经尝试过使用它,但它仍然将src设置为文件路径而不是它显示的URL – CMS2

0

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

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

UPDATE:没有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> 
+0

好吧,我想稍微澄清一下object.src,我试图避免使用超链接|更喜欢按钮 – CMS2

+0

@ CMS2我编辑了答案,请看看。关于'obj。src','iload(this)'将解析iframe对象到obj变量中。所以当你调用'obj.src'时,它会返回iframe的'src'。 – Aister

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

谢谢所有谁回答