javascript 网页自动跳转

<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<button>点击跳转</button>
<div></div>
<script>
    var btn = document.querySelector('button');
    btn.addEventListener('click', function () {   // 注册按钮点击事件
        location.href = "http://www.baidu.com";  //  要跳转到的地址
    });
    var div = document.querySelector('div');
    var time = 5;   // 这里设置自动跳转的时间

    function f() {
        if (time > 0) {
            div.innerHTML = time + "s后跳转页面";
            time--;
        } else {
            location.href = 'http://www.baidu.com';  // 要跳转到的地址
        }
    }

    setInterval(f, 1000);  // 计时器

</script>
</body>
</html>

猜你喜欢

转载自www.cnblogs.com/nigori/p/10770569.html