2011-05-15 46 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