Home  >  Article  >  Web Front-end  >  How to add jump function to button using JS (similar to a tag)

How to add jump function to button using JS (similar to a tag)

黄舟
黄舟Original
2017-05-31 10:19:312680browse

This article mainly introduces JS to add a jump function similar to a tag to the button. Friends who need it can refer to the

method

window.location.href = "要跳转的URL";

or

window.location = "要跳转的URL";

or

location = "要跳转的URL";

example

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>给按钮添加跳转功能【类似a标签】</title>
    <script type="text/javascript">
      window.onload = function(){
        document.getElementById("btn").onclick = function(){
          // window.location.href = "https://www.baidu.com/";
          // window.location = "https://www.baidu.com/";
          location = "https://www.baidu.com/";
        };
      };
    </script>
  </head>
  <body>
    <input id="btn" type="button" value="百度一下,你就知道" />
  </body>
</html>

The above is the detailed content of How to add jump function to button using JS (similar to a tag). 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