javascript 等待两秒钟,然后在jquery中自动重定向另一个页面?

23c0lvtd  于 2023-01-04  发布在  Java
关注(0)|答案(6)|浏览(113)

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标签页。我该怎么做?

ugmeyewa

ugmeyewa1#

您甚至不需要jQuery;香草JS会很好用...

<a id="link" href="http://example.com">Go NOW!!!</a>

联森:

window.setTimeout(function() {
location.href = document.getElementsByClassName("user_register border_y")[0].getElementsByTagName("a")[0].href;
}, 2000);

getElementsByClassName并不是在所有浏览器中都能正常工作;因此请确保提供了fallback

yebdmbv4

yebdmbv42#

使用setTimeout

setTimeout(function() { 
    window.location.href = $("a")[0].href; 
 }, 2000);
iqjalb3h

iqjalb3h3#

为什么使用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>
sg3maiej

sg3maiej4#

下面是根据您的描述编写的简单脚本:

function redirect(){
   window.location = $('a').attr('href');
}

setTimeout(redirect, 2000);

演示:http://jsfiddle.net/KesU9/

5ssjco0h

5ssjco0h5#

body OnLoad上使用此简单代码

<body onLoad="setTimeout(location.href='http://www.newpage.com', '2000')">
ubby3x7f

ubby3x7f6#

使用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>

在body选项卡后添加此代码

<h3>Auto redirect after <span id="timecount"></span> second(s)!</h3>

相关问题