无法设置属性与JavaScript

问题描述:

以及这是我的问题,这可能是我自己非常明显和愚蠢的错误,我似乎无法注意到,我没有用JavaScript很多。 我认为这段代码很明显,我显示3个.swf文件,并且根据生成3个随机数的脚本的结果,我想编辑嵌入式Flash动画的“src”和“value”属性。无法设置属性与JavaScript

我试图通过使用setAttribute和公正element.value都=“ECC ..” 没有工作,属性保持不变

<!DOCTYPE HTML> 

<html> 
<head> 
<title>example</title> 
<link rel="stylesheet" type="text/css" href="styles.css" /> 
</head> 

<body> 

<script type="text/javascript"> 
var prereel1=Math.floor(Math.random()*25); 
var reel1="flash/"+ prereel1 + ".swf"; 
var prereel2=Math.floor(Math.random()*25); 
var reel2="flash/"+ prereel2 + ".swf"; 
var prereel3=Math.floor(Math.random()*25); 
var reel3="flash/"+ prereel3 + ".swf"; 
document.getElementById("slot1").value=reel1; 
document.getElementById("slot2").setAttribute("value", reel2); 
document.getElementById("slot3").setAttribute("value", reel3); 
document.getElementById("eslot1").src=reel1; 
document.getElementById("eslot2").setAttribute("src", reel2); 
document.getElementById("eslot3").setAttribute("src", reel3); 
</script> 

<div id="slots"> 

< object width="150" height="210"> 
<param id="slot1" name="movie" value="flash/1.swf"> 
<embed id="eslot1" src="flash/1.swf" width="150" height="210"> 
</embed> 
</object><object width="150" height="210"> 
<param id="slot2" name="movie" value="flash/1.swf"> 
<embed id="eslot2" src="flash/1.swf" width="150" height="210"> 
</embed> 
</object><object width="150" height="210"> 
<param id="slot3" name="movie" value="flash/1.swf"> 
<embed id="eslot3" src="flash/1.swf" width="150" height="210"> 
</embed> 
</object> 

</div> 

</body> 
</html> 
+1

如果你要执行立即将JavaScript代码放在**内容后面。 – 2011-04-18 00:22:23

你的脚本放在Flash对象的上方。

页面按顺序装入。一旦它到达你的脚本,它就试图在DOM中找到id为“slot1”的对象,并且找不到任何对象,因为它尚未加载它们。

所以你想利用你的脚本,并把它的内容

<div id="slots"> 

<object width="150" height="210"> 
<param id="slot1" name="movie" value="flash/1.swf"> 
<embed id="eslot1" src="flash/1.swf" width="150" height="210"> 
</embed> 
</object><object width="150" height="210"> 
<param id="slot2" name="movie" value="flash/1.swf"> 
<embed id="eslot2" src="flash/1.swf" width="150" height="210"> 
</embed> 
</object><object width="150" height="210"> 
<param id="slot3" name="movie" value="flash/1.swf"> 
<embed id="eslot3" src="flash/1.swf" width="150" height="210"> 
</embed> 
</object> 

</div> 

<script type="text/javascript"> 
var prereel1=Math.floor(Math.random()*25); 
var reel1="flash/"+ prereel1 + ".swf"; 
var prereel2=Math.floor(Math.random()*25); 
var reel2="flash/"+ prereel2 + ".swf"; 
var prereel3=Math.floor(Math.random()*25); 
var reel3="flash/"+ prereel3 + ".swf"; 
document.getElementById("slot1").value=reel1; 
document.getElementById("slot2").setAttribute("value", reel2); 
document.getElementById("slot3").setAttribute("value", reel3); 
document.getElementById("eslot1").src=reel1; 
document.getElementById("eslot2").setAttribute("src", reel2); 
document.getElementById("eslot3").setAttribute("src", reel3); 
</script> 

低于或者,你可以在onload块包装你的代码。

<script type="text/javascript"> 
window.onload = function() { 
    // code here 
}; 
</script> 

这意味着代码仅在页面加载完成时运行。然后你的Flash对象将在DOM中,你可以访问它们。

+0

哦。那对我来说确实很愚蠢。 – Xaendro 2011-04-18 00:22:57

+0

@Xaendro别担心,每个人都会犯简单的错误。如果它解决了你的问题,不要忘记点击勾号并将答案标记为接受的答案:) – Raynos 2011-04-18 00:24:26

就像Raynos说的那样,你的脚本是在对象实际存在之前运行的。

您可以封装在一个函数代码,然后用在window.onload来触发功能,即: 功能randomizeMovies(){// 你的东西 }

的window.onload = randomizeMovies; 。

您已经执行完毕的答案一个和两个后,

的document.getElementById( 'SLOT1')SRC = “嗒嗒”; 或更确定的方式: document.getElementById('slot1')。setAttriibute('src',“blah”);

正如你所做的那样,两种方式都有效(不能确定),但后者是跨浏览器,我相信(绝对)。

是的,JavaScript代码似乎只有当它被放在它正在访问的元素后才有效。曾经有一段时间我已经看到头部元素的工作,但事情并不一致,我还没有找到一个解决方案,以确保浏览器已加载页面。也许一个while循环检查页面是否使用计时器加载,以免导致浏览器“失控脚本”错误?我今天应该修改这个概念。

(啊 - 丹尼·古德曼的书说,在文档的载入过程中执行脚本应有助于生成文档及其对象,他给出了没有代码,以便避免onload事件。)