0

好吧,这是我的问题,这可能是我自己的非常明显和愚蠢的错误,我似乎没有注意到,我没有太多使用 javascript。我认为这段代码非常明显,我正在显示 3 个 .swf 文件,并且根据生成 3 个随机数的脚本的结果,我想编辑嵌入式 Flash 电影的“src”和“value”属性。

我尝试使用 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>
4

3 回答 3

2

您的脚本放置在 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 中,您可以访问它们。

于 2011-04-18T00:21:19.290 回答
2

就像 Raynos 所说,您的脚本在对象实际存在之前运行。

您可以将代码封装在一个函数中,然后使用 window.onload 来触发该函数,即: function randomizeMovies() { // your stuff }

window.onload = 随机电影;

于 2011-04-18T00:25:27.233 回答
1

在你完成答案一和二之后,

document.getElementById('slot1').src="blah"; 或更确定的方式: document.getElementById('slot1').setAttriibute('src', "blah");

正如您所做的那样,任何一种方式都有效(无法确定),但我相信后者是跨浏览器(肯定)。

是的,javascript 代码似乎只有在它正在访问的元素之后才起作用。偶尔我看到在 head 元素中工作正常,但那里的工作并不一致,我还没有找到确保浏览器加载页面的解决方案。也许是一个while循环检查页面是否使用计时器加载,以免导致浏览器“失控脚本”错误?我今天应该修改这个概念。

(啊 - 丹尼古德曼的书说,在文档加载期间执行的脚本应该有助于生成文档及其对象。他没有为此提供代码。所以避免加载)

于 2011-04-18T03:30:57.087 回答