为什么我每次点击登录按钮时程序都冻结

问题描述 投票:0回答:1

所以我有这个程序,因此您可以通过将书签作为链接并将其保存在localStorage中来对书签进行密码保护。问题是当我按下登录按钮(该按钮检查您是否具有正确的用户名和密码)以及具有正确的用户名和密码时,程序将冻结,我无法弄清楚原因。 localStorage.getItem('number')设置为0。

function n() {
  //pull the number of links you have from local storage and add 1 and then put it back in
  var num = parseInt(localStorage.getItem('number'), 10);
  num++;
  localStorage.setItem('number', num.toString());
  return num.toString();
}

function register() {
  //changes what the screen looks like so you can create an account
  $("body").html("<input id='u2' placeholder='username'><br><input id='p2' type='password' placeholder='password'><br><button onclick='y()'>create account</button>")
}

function login() {
  //does the same thing as the register but you login instead of signing up and if you don't have an account makes you create one
  if (localStorage.getItem('password') == null || localStorage.getItem('username') == null) {
    alert("you do not seem to have an account")
    register();
  }
  $("body").html("<input id='u' placeholder='username'><br><input id='p'type='password' placeholder='password'><br><button onclick='x()'>login</button>")
}

function x() {
  //checks the username and password and runs the place function to place the links
  if (localStorage.getItem('username') === $('#u').val() && localStorage.getItem('password') === $('#p').val()) {
    $("body").html("<h1>Hello " + localStorage.getItem('username') + " </h1><br><button onclick='add()'>add a bookmark</button>")
    place();
  }
}

function y() {
  //creates account
  localStorage.setItem('username', $("#u2").val())
  localStorage.setItem('password', $("#p2").val())
  $("body").html("<h1>Hello " + localStorage.getItem('username') + "</h1><br><button onclick='add()'>add a bookmark</button>")
}

function add() {
  //creates the links to be added
  var word = prompt("What would you like to see as your link");
  var href = prompt("where would you like to have the link to go");
  var a = $("<a></a>");
  a.prop("href", "https://" + href)
  a.prop("target", "_blank")
  a.html(word)
  $("body").append(a)
  localStorage.setItem(n(), a.toString());
}

function place() {
  //places the links that were saved in localStorage on the website (without this in the x function this works perfectly)
  for (var i = 0; i <= parseInt(n(), 10); i++) {
    $("body").append(localStorage.getItem(i.toString()));
  }
}
<button onclick="login()">login</button>
    <button onclick="register()">register</button>
javascript local-storage
1个回答
0
投票

有2种不同的选择。一个可以注册到网站,另一个可以登录到网站。如果同时包含HTML和Javascript / jQuery,这会有所帮助吗?

© www.soinside.com 2019 - 2024. All rights reserved.