如何使用 JavaScript 更改 embed src 的值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6646413/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 21:23:13  来源:igfitidea点击:

How to change the value of embed src with JavaScript?

javascriptembedsrc

提问by Dr.Molle

I have just begun to learn JS. I am trying to change the value of embed src in the tag present in my HTML code. But am unable to do so with the following code I've written -

我刚刚开始学习JS。我正在尝试更改 HTML 代码中存在的标记中 embed src 的值。但是我无法使用我编写的以下代码这样做 -

HTML -

HTML -

<ol>
<li><a href="http://embedgames.ru/wp-content/games/kitty-throw.swf" 
    onclick="showGame(this);return false;">Kitty Throw</a></li>
</ol>

<embed id="gameHolder" src="http://pictat.com/i/2011/7/10/32479playscrnba.jpg" 
    quality="high" menu ="false" width="550" height="400" 
    type="application/x-shockwave-flash" 
    pluginspage="http://www.macromedia.com/go/getflashplayer" /></center>

JS:

JS:

function showGame(whichgame){var source=whichgame.getAttribute("href");
var game=document.getElementById("gameHolder");
game.setAttribute("src",source);}

I want the JS to display the flash file selected in the gameHolder space which by default holds an image. I am unable to do this with just my beginner knowledge of JS, also please explain the code as you use it.

我希望 JS 显示在默认情况下保存图像的 gameHolder 空间中选择的 Flash 文件。仅凭我的 JS 初学者知识我无法做到这一点,还请在使用时解释代码。

回答by Dr.Molle

It may depend on the browser and the type of the embedded object, how you have to change the object(for example there are special methods for flash-movies like Play() , but the object isn't a flash-movie at the begin)

这可能取决于浏览器和嵌入对象的类型,您必须如何更改对象(例如,有像 Play() 这样的 Flash 电影的特殊方法,但该对象一开始并不是 Flash 电影)

A common way is to replace the whole embed-node with a new <embed>:

一种常见的方法是用一个新的 embed-node 替换整个 embed-node <embed>

function showGame(whichgame){
  var source=whichgame.getAttribute("href");
  var game=document.getElementById("gameHolder");
  var clone=game.cloneNode(true);
  clone.setAttribute('src',source);
  game.parentNode.replaceChild(clone,game)
}

回答by Kashyap Vyas

you can set src of embed tag in javascript, for that you must have to write your embed tag in javascript like below example :

您可以在 javascript 中设置 embed 标签的 src,为此您必须在 javascript 中编写您的 embed 标签,如下例所示:

function onclickofSomething() {
    $('#IDOfParentElement').html("<embed type='application/x-mplayer2' pluginspage='http:///www.microsoft.com/Windows/MediaPlayer/' src='" + "<%=YourVideoPath%>" + "YourVideoName" + ID + ".mp4/wmv" + "' autostart='1' showstatusbar='1' enabled='1' showdisplay='1' showcontrols='1' width='630' height='380'></embed>");
}

you can also see below url : need to set video file name using javascript http://www.webdeveloper.com/forum/showthread.php?53086-how-to-change-the-src-in-lt-embed-gt-using-javascripthttp://www.daniweb.com/web-development/javascript-dhtml-ajax/threads/16626/how-to-change-the-the-value-of-src-in-embed-using-javascript

您还可以在下面的 url 中看到:需要使用 javascript http://www.webdeveloper.com/forum/showthread.php?53086-how-to-change-the-src-in-lt-embed-gt设置视频文件名-using-javascript http://www.daniweb.com/web-development/javascript-dhtml-ajax/threads/16626/how-to-change-the-the-value-of-src-in-embed-using- javascript

回答by Александр Бурундук

It works also if you change param src only

如果您仅更改参数 src,它也适用

<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000">
 <EMBED id="movie" src="first.swf" "></EMBED>
</OBJECT>
<ul>
 <li name='moviename'>first.swf</li>
 <li name='moviename'>second.swf</li>
 <li name='moviename'>third.swf</li>
</ul>
<script type="text/javascript">
 var names=document.getElementsByName("moviename");
 for (var i = names.length - 1; i >= 0; i--) {
  names[i].addEventListener("click", myFunction);
  function myFunction() {
      document.getElementById("movie").setAttribute("src", this.innerHTML);
  }
 }
</script>