2011-05-15 45 views
2

需要幫助我的簡單腳本,應該在5秒後自動點擊下一個。Javascript onload運行功能幫助

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> 
<title> 
</title> 
<script type="text/javascript"> 
window.onload = Next() { 
    setTimeout(Next() { 
     var next = document.getElementById("NEXT"); 
     window.location.href = next.href; 
    }, 5000); 
} 
</script> 

</head> 
<body> 
<div align="right"> 
<a id="NEXT" href="http://www.mysite.com/pictures.php?id=34">[ NEXT ]</a> 
</div> 
</body> 
</html> 
+1

現在,是什麼問題? – jams 2011-05-15 00:16:17

+0

是你的代碼不工作? – jams 2011-05-15 00:17:50

+0

@Thomas'click()'不起作用;) – Raynos 2011-05-15 00:20:06

回答

3

你的問題是,.click()只適用於按鈕。

雖然在它讓我們使用不顯眼的JavaScript。

window.onload = function() { 
    setTimeout(function() { 
     var next = document.getElementById("NEXT") 
     window.location.href = next.href; 
    }, 5000); 
} 

Live example

編輯

window.onload = Next() { 
setTimeout(Next() { 

不要使用這個詞Next()只使用function()

要創建你需要或者function()function SomeName()

+0

您好Raynos我添加了您的腳本,它仍然無法正常工作。不知道我是否粘貼錯了。 – Ryan 2011-05-15 00:55:58

+0

@Ryan你把它包裝在''? – Raynos 2011-05-15 00:58:37

+0

我更新了我的原始腳本供您查看,非常感謝任何幫助。 – Ryan 2011-05-15 01:04:07

0

在正確的時候,你的腳本沒有按功能不要說它是JavaScript(你需要說它是哪種腳本語言),並且你的html在技術上不會說它是HTM L(它缺少DOCTYPE聲明):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html> 
<head> 
    <title>Title of the document</title> 
    <script type="text/javascript"> 
    function next() { 
     // '1' dynamically generated when this page was generated by PHP, 
     // and will be '2' next time the page loads. 
     location = "pictures.php?id=1"; 
    } 
    document.addEventListener("DOMContentLoaded", function(){setTimeout(next,5000);}, false); 
    </script> 
</head> 
<body> 
    ... 
</body> 
</html> 

這聽起來就像是迂腐,但IE尤其是真正的肛門約擁有這些東西。如果沒有文檔類型聲明,它不會將以HTML代碼開頭的文檔對待,但會開始採取相當不準確的猜測。

+0

'document.addEventListener(「DOmContentLoaded」'不支持IE – Raynos 2011-05-15 00:32:30