2014-03-19 88 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

謝謝所有誰回答