不能使用javascript设置属性

| 好吧,这是我的问题,这可能是我自己非常明显和愚蠢的错误,我似乎看不到,我还没有使用太多的javascript。 我认为这段代码非常明显,我正在显示3个.swf文件,并且根据生成3个随机数的脚本的结果,我想编辑以下代码的\“ src \”和\“ value \”属性嵌入式Flash电影。 我尝试使用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>
    
已邀请:
您的脚本位于Flash对象上方。 页面按顺序加载。一旦到达脚本,它就会尝试在id为“ slot1 \”的DOM中找到该对象,但找不到任何对象,因为尚未加载它们。 因此,您想使用脚本并将其放置在内容下方
<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中,并且您可以访问它们。     
就像Raynos所说的那样,您的脚本在对象实际存在之前就已运行。 您可以将代码封装在一个函数中,然后使用window.onload触发该函数,即: 函数randomizeMovies(){   // 你的东西 } window.onload = randomizeMovies;     
完成答案一和二后, document.getElementById(\'slot1 \')。src = \“ blah \”; 或更确定的方法: document.getElementById(\'slot1 \')。setAttriibute(\'src \',\“ blah \”); 正如您所做的那样,两种方法都可以(肯定不会记住),但是我相信(肯定)后者是跨浏览器的。 是的,JavaScript代码似乎仅在将其放在要访问的元素之后时才起作用。偶尔我会看到在head元素中一切正常,但在那里却无法始终如一地工作,而且我还没有找到一种解决方案来确保浏览器已加载页面。也许有一段时间循环使用计时器检查页面是否已加载,以免导致浏览器“脚本失控”错误?我今天应该修正这个概念。 (啊-丹尼·古德曼的书说,在文档加载期间执行的脚本应该有助于生成文档及其对象。他没有为此提供任何代码。因此请避免onload)     

要回复问题请先登录注册