簡體   English   中英

Javascript嵌入腳本onclick

[英]Javascript Embedding Scripts onclick

我想做的是基於在同一頁面上單擊的不同鏈接來更改div的內容。 誰能指出我正確的方向? AFAIK將腳本直接插入到頁面中可能很危險,更改文本效果還可以,但是似乎我不確定腳本。 腳本的內容是用於視頻流的嵌入代碼。 我意識到這可能不是正確的解決方法。 我的嘗試無法成功,因為轉義了'<,>'字符並僅傳遞參數似乎接受不帶空格的文本。

我嘗試過的方式如下(用偽代碼表示);

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>')>

您可以放棄使用JavaScript,並創建一個具有指定名稱的iFrame來托管內容。 同時為鏈接指定目標標簽。 因此,具有指定目標標記的所有鏈接都會出現在命名的iFrame中。

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

我建議您使用不引人注目的Javascript來定位a元素,例如,使用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屬性中。

告訴我進展如何,問候。

我可能把這個問題概括得太多了。

因此,我想在單擊鏈接時嵌入流。 如果該鏈接類似於正確的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方法由於轉義<字符而無法工作,並且傳遞嵌入代碼似乎是問題所在。 希望我已經更清楚了嗎?

<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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM