2011-08-31 63 views
0

我想要做的就是根據點擊在同一頁上的不同鏈接改變div的內容。任何人都可以指出我正確的方向嗎? AFAIK將腳本直接插入到頁面中可能會很危險,改變文本的作用不錯,但似乎我不確定腳本。腳本的內容嵌入了視頻流的代碼。我意識到這可能不是正確的做法。我的嘗試不會工作,因爲要逃避'<,>'字符並且傳遞參數似乎只接受沒有空格的文本。Javascript嵌入腳本onclick

我試過的方法如下(以僞碼形式);

function changeVideo(script){ div.innerhtml=script;} 

然後改變div的內容的鏈接;

<a href='#' onclick=changeVideo('<iframe src=justin.tv etc..></iframe>')> 
<a href='#' onclick=changeVideo('<iframe src=ustream.tv etc..></iframe>')> 
+0

而不是更改div的'innerhtml',您可以更容易,只需更改iframe的src。 'function changeVideo(vid_src){document.getElementById('vid_iframe')。src = vid_src; }' –

+0

正如肖恩在他的回答中所說的,使用* target *屬性,不需要腳本。 – RobG

回答

2

您可以刪除使用JavaScript,並用指定主辦的內容創建一個iFrame;同時給鏈接一個目標標籤。因此,使指定指定的目標的任何鏈接出現在指定的iFrame內。

但是,如果你堅持使用JavaScript,你可以考慮使用AJAX。

+1

我想你是指[target](http://www.w3.org/TR/html401/present/frames.html#adef-target)**屬性**。 – RobG

+0

的確我是Rob。 –

1

我建議你找到你的a元素與unobstrusive的Javascript,與getElementById()例如。

一旦你在變量一樣得到了他們,讓說,A1A2,而iframe是可變IFRAME做這樣的代碼。

// ...a1 and a2 are anchors and iframe is the frame. 
var srcSwitch = function(e) 
{ 
    e.preventDefault(); // this will prevent the anchor from redirecting you 
    iframe.src = this.src; // this changes the iFrame‘s source 
}; 
a1.addEventListener('click', srcSwitch, 1); 
a2.addEventListener('click', srcSwitch, 1); // and we register event listeners. 

使用此代碼,不需要在HTML屬性中插入Javascript,並且只能將腳本URL置於錨定SRC屬性中。

告訴我它是怎麼回事,問候。

0

我可能已經概括了太多的問題。

所以我想嵌入一個流點擊鏈接。如果鏈接類似於正確的URL http://Jimey.tv/mystream iframe可以工作,但會加載整個頁面。但我想使用流嵌入代碼來獲取播放器

嵌入代碼看起來類似於;

<script type = text/javascript> channel='mychannel' w='620' h='400'</script><script type=text/javascript> src="jimmy.tv/embed.js></script> 

我原來的JavaScript方法不會因爲逃避<字符,並傳遞embedcode的工作似乎是problamatic。希望我已經做得更清楚了?

+0

請用目前的代碼顯示一個示例網址,否則不可能知道您正在嘗試做什麼,因爲您的問題太模糊。 – 2011-08-31 20:09:18

0
<script> 
iframe1 = '<iframe frameborder="0" scrolling="no" id="chat_embed" src="http://twitch.tv/chat/embed?channel=xenema&amp;popout_chat=true" height="301" width="221"></iframe>' 
</script> 
<a href="#" onclick="alert(222)">link 1</a> 
<a href="#" onclick="document.getElementById('videos').innerHTML = iframe1">link 2</a> 
<div id="videos">diiiiv</div>