更改src属性

时间:2012-10-19 00:16:38

标签: javascript html

我想更改嵌入对象的src属性。我有:

<embed class="flash "id="flash" src="swf/pano.swf?panoSrc=images/a.jpg" 
                     allowFullScreen="true" 
                     width="1280" height="640" quality="high" 
                     pluginspage="http://www.macromedia.com/go/getflashplayer" 
                     type="application/x-shockwave-flash" bgcolor="#DDDDDD"/>

我试过

function change() 
{
   document.getElementById("flash").setAttribute('src', 'swf/pano.swf?panoSrc=b.jpg');
}

但它在Chrome中不起作用,“仅限”在Firefox,IE和Opera中。我该如何修复它(使其在Chrome中运行)?我做错了吗?

编辑:这是整个HTML文件

<!DOCTYPE html>

<html lang="en">
<head>
    <meta charset="utf-8" />
    <title></title>
    <script>
        function change() {
            document.getElementById("flash").setAttribute('src', 'swf/pano.swf?panoSrc=a.jpg');
        }
    </script>

</head>
<body>


    <embed class="flash" id="flash" src="swf/pano.swf?panoSrc=images/sau.jpg" allowFullScreen="true" 
                     width="1280" height="640" quality="high" 
                     pluginspage="http://www.macromedia.com/go/getflashplayer" 
                     type="application/x-shockwave-flash" bgcolor="#DDDDDD"/>

     <script>
         change();
     </script>

</body>

链接到页面:http://chin.comli.com/test.html

3 个答案:

答案 0 :(得分:3)

您需要调用您的函数。

change();

工作演示:http://jsfiddle.net/LUfUx/

答案 1 :(得分:2)

将脚本调用移动到它想要影响的代码之后,或将其放在window.onload调用中。正如你所拥有的那样,它试图在元素存在之前执行它。

长话短说,基于Changing object embed source via jqueryDynamically change embedded video src in IE/Chrome (works in Firefox)JavaScript: Changing src-attribute of a embed-taghttp://code.google.com/p/chromium/issues/detail?id=69648,看起来这是Chrome中的一个错误,唯一的解决方法是删除整个<embed>元素,然后使用更改的src重新插入它。

答案 2 :(得分:0)

试试这个

function change() {
   document.getElementById("flash").src =  'swf/pano.swf?panoSrc=b.jpg';
   // OR  document["flash"].src =  'swf/pano.swf?panoSrc=b.jpg';
}