Home  >  Article  >  Web Front-end  >  How to implement button click to jump in javascript

How to implement button click to jump in javascript

青灯夜游
青灯夜游Original
2021-04-20 15:13:036358browse

Method: 1. Add the "onclick="location.href='url'"" statement to the button element to jump. 2. First bind the click event to the button element; then specify the function that will be executed when the click event is triggered; finally, in the execution function, use the "location.href" statement to set the jump function.

How to implement button click to jump in javascript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

Method 1: Use location.href directly on the button element to jump

<input type="submit" name="Submit" value="同意" onclick="location.href=&#39;http://www.php.cn&#39;">

Method 2: Give Bind the click event to the button element and jump

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <button>点点点</button>
    <div></div>
    <script>
        var btn = document.querySelector(&#39;button&#39;)
        var div = document.querySelector(&#39;div&#39;)
        btn.addEventListener(&#39;click&#39;, function () {
            // location.href = &#39;http://www.baidu.com&#39;;
            var timer = 5;
            setInterval(function () {
                if (timer == 0) {
                    location.href = &#39;http://www.php.cn&#39;;
                } else {
                    div.innerHTML = &#39;你还有&#39; + timer + &#39;秒就可以跳转了&#39;;
                    timer--
                }
            },1000)
            /*  setInterval(function(){
                 div.innerHTML = &#39;你还有&#39; + timer + &#39;秒就可以跳转了&#39;
                 timer--;
             },1000) */
        });
    </script>
</body>

</html>

[Recommended learning: javascript advanced tutorial]

The above is the detailed content of How to implement button click to jump in javascript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn