html结构:
<div class="user_register border_y">
<div>
<span style="color:green;"> after two seconds then auto redirect </span><br><br>
<a href="http://example.com/down/test.html">if you don't want to wait you can click this。</a></div></div>
我想使用jquery获取:两秒钟后自动重定向到a
标签页面。我该怎么办?
答案 0 :(得分:15)
你甚至不需要jQuery;香草JS工作正常......
<a id="link" href="http://example.com">Go NOW!!!</a>
JS:
window.setTimeout(function() {
location.href = document.getElementsByClassName("user_register border_y")[0].getElementsByTagName("a")[0].href;
}, 2000);
getElementsByClassName
无法在所有浏览器中使用;所以请务必提供fallback
答案 1 :(得分:14)
使用setTimeout
:
setTimeout(function() {
window.location.href = $("a")[0].href;
}, 2000);
答案 2 :(得分:11)
为什么要使用JavaScript?
<head>
<meta http-equiv="refresh" content="2;url=http://example.com/down/test.html">
</head>
<body>
<div class="user_register border_y">
<div>
<span style="color:green;"> after two seconds then auto redirect </span><br><br>
<a href="http://example.com/down/test.html">if you don't want to wait you can click this。</a></div></div>
答案 3 :(得分:4)
以下是基于您的描述的简单脚本:
function redirect(){
window.location = $('a').attr('href');
}
setTimeout(redirect, 2000);
答案 4 :(得分:1)
在 body OnLoad
上使用此简单代码<body onLoad="setTimeout(location.href='http://www.newpage.com', '2000')">
答案 5 :(得分:0)
使用JavaScript重定向。 例如:
<script type="text/javascript">
var time = 15; // Time coutdown
var page = "http://www.redirect-url.com/";
function countDown(){
time--;
gett("timecount").innerHTML = time;
if(time == -1){
window.location = page;
}
}
function gett(id){
if(document.getElementById) return document.getElementById(id);
if(document.all) return document.all.id;
if(document.layers) return document.layers.id;
if(window.opera) return window.opera.id;
}
function init(){
if(gett('timecount')){
setInterval(countDown, 1000);
gett("timecount").innerHTML = time;
}
else{
setTimeout(init, 50);
}
}
document.onload = init();
</SCRIPT>
在正文标签
之后添加此代码<h3>Auto redirect after <span id="timecount"></span> second(s)!</h3>