Javascript:点击按钮加载HTML页面

时间:2018-01-29 01:11:38

标签: javascript jquery html

我在HTML中定义了button

我想在点击此按钮时加载页面background.html。但我想从javascript而不是从.html文件中做到这一点。所以在我的register.js文件中,我有以下内容:

$(document).ready(function() {
  $("#btnApply").click(function() {
    window.open("background.html");
  })
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="button" id="btnApply" value="Register">

但这不会加载页面。我错过了什么?

3 个答案:

答案 0 :(得分:0)

可能是因为该页面上的弹出窗口被阻止了。您只需要允许它们(来自Chrome的屏幕截图):

enter image description here

确保你:

  1. 在浏览器中运行此代码(否则您将没有window对象)

  2. background.html与您的脚本位于同一文件夹中

答案 1 :(得分:0)

<input type = "button" id = "btnApply" value = "Register" onclick="loadPage()">

然后该函数可以在您的register.js文件中:

    function loadPage()
{

     window.location="background.html";

}

答案 2 :(得分:0)

open()方法会打开一个新的浏览器窗口。如果您希望从同一窗口加载,请使用location对象。

window.location.assign("background.html");
相关问题