2011-04-18 113 views
0

以及這是我的問題,這可能是我自己非常明顯和愚蠢的錯誤,我似乎無法注意到,我沒有用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

回答

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中,你可以訪問它們。

+0

哦。那對我來說確實很愚蠢。 – Xaendro 2011-04-18 00:22:57

+0

@Xaendro別擔心,每個人都會犯簡單的錯誤。如果它解決了你的問題,不要忘記點擊勾號並將答案標記爲接受的答案:) – Raynos 2011-04-18 00:24:26

2

就像Raynos說的那樣,你的腳本是在對象實際存在之前運行的。

您可以封裝在一個函數代碼,然後用在window.onload來觸發功能,即: 功能randomizeMovies(){// 你的東西 }

的window.onload = randomizeMovies; 。

0

您已經執行完畢的答案一個和兩個後,

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

正如你所做的那樣,兩種方式都有效(不能確定),但後者是跨瀏覽器,我相信(絕對)。

是的,JavaScript代碼似乎只有當它被放在它正在訪問的元素後纔有效。曾經有一段時間我已經看到頭部元素的工作,但事情並不一致,我還沒有找到一個解決方案,以確保瀏覽器已加載頁面。也許一個while循環檢查頁面是否使用計時器加載,以免導致瀏覽器「失控腳本」錯誤?我今天應該修改這個概念。

(啊 - 丹尼·古德曼的書說,在文檔的載入過程中執行腳本應有助於生成文檔及其對象,他給出了沒有代碼,以便避免onload事件。)