Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 更改src属性_Javascript_Html - Fatal编程技术网

Javascript 更改src属性

Javascript 更改src属性,javascript,html,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.ma

我想更改嵌入对象的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"/>
但它在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>

函数更改(){
document.getElementById(“flash”).setAttribute('src','swf/pano.swf?panoSrc=a.jpg');
}
改变();

链接到页面:

试试这个

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

您需要调用您的函数

change()

工作演示:

将脚本调用移动到它要影响的代码之后,或者将其放在window.onload调用中。正如您所看到的,它试图在元素存在之前在其上执行


长话短说,基于、和,看起来这是Chrome中的一个bug,唯一的解决方法是删除整个
元素,然后用更改的src重新插入它。

为什么不删除嵌入标记呢。。插入新的源代码并再次将其注入DOM。。也许这也有助于检查这需要在
嵌入之后进行,这样
文档.getElementById
就可以在代码中找到它。是的,bree说了。在结束标记之前,它通常是最好的,但在Chrome中不起作用,在Firefox、IE和Opera中“仅”起作用。这是链接:我明白了。但即使这样做了,它仍然不起作用。我编辑了代码。这就是你的意思吗?不要使用webmatrix来修复这个问题,在Chrome或Firefox中调试它,并使用内置的开发工具或插件来查看任何错误。正如你从这把小提琴上看到的,代码是有效的,所以还有其他一些事情。哦,我刚刚发现它在firefox中有效,但在Chrome中不起作用,我总是使用Chrome来预览结果。很奇怪…我如何在Chrome中使用开发人员的工具?抱歉,我完全是新手,点击F12打开它们并选择控制台。然后重新加载页面。
function change() {
   document.getElementById("flash").src =  'swf/pano.swf?panoSrc=b.jpg';
   // OR  document["flash"].src =  'swf/pano.swf?panoSrc=b.jpg';
}