简体   繁体   English

Javascript嵌入脚本onclick

[英]Javascript Embedding Scripts onclick

What I would like to do is change the content of a div based on the different links clicked on the same page. 我想做的是基于在同一页面上单击的不同链接来更改div的内容。 Can anyone point me in the correct direction? 谁能指出我正确的方向? AFAIK it could be dangerous to insert scripts directly into a page, changing text works okay but it seems I'm not sure about scripts. AFAIK将脚本直接插入到页面中可能很危险,更改文本效果还可以,但是似乎我不确定脚本。 The content of the scripts are embed codes for video streaming. 脚本的内容是用于视频流的嵌入代码。 I realise this may not be the right way to go about it. 我意识到这可能不是正确的解决方法。 My attempt won't work because of escaping the '<,>' characters and passing the parameter only seems to accept text with no spaces. 我的尝试无法成功,因为转义了'<,>'字符并仅传递参数似乎接受不带空格的文本。

The way I've attempted it is as follows (in pseudocode); 我尝试过的方式如下(用伪代码表示);

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

then links that change the content of the div; 然后更改div内容的链接;

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

You could drop the use of JavaScript and create an iFrame with a specified name to host the content; 您可以放弃使用JavaScript,并创建一个具有指定名称的iFrame来托管内容。 while giving the links a target tag. 同时为链接指定目标标签。 Thus making any links with the target tag specified appear within the named iFrame. 因此,具有指定目标标记的所有链接都会出现在命名的iFrame中。

However if you insist upon using JavaScript you could consider the use of AJAX. 但是,如果您坚持使用JavaScript,则可以考虑使用AJAX。

I suggest you to locate your a elements with unobstrusive Javascript, with getElementById() for example. 我建议您使用不引人注目的Javascript来定位a元素,例如,使用getElementById()。

Once you have got them in variables like, lets say, a1 and a2 , and the iFrame is in variable iframe do a code like this. 一旦将它们放入变量中,比如说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.

With this code, there is no need to insert Javascript within HTML attributes and you must only put the script URL in the anchors SRC attributes. 使用此代码,无需在HTML属性中插入Javascript,而只需将脚本URL放入锚点SRC属性中。

Tell me how it goes, greetings. 告诉我进展如何,问候。

I may have generalised the question too much. 我可能把这个问题概括得太多了。

So I want to embed a stream on clicking a link. 因此,我想在单击链接时嵌入流。 If the link is something like a proper URL http://Jimey.tv/mystream the iframe works, but loads the whole page. 如果该链接类似于正确的URL http://Jimey.tv/mystream,则iframe可以运行,但会加载整个页面。 But I want to use the stream embed code to get just the player 但是我想使用流嵌入代码来获取播放器

The embed code looks similar to; 嵌入代码看起来类似于;

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

My original JavaScript method doesn't work because of escaping the < characters, and passing the embedcode seems to be problamatic. 我的原始JavaScript方法由于转义<字符而无法工作,并且传递嵌入代码似乎是问题所在。 Hopefully I've made this a bit clearer? 希望我已经更清楚了吗?

<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