从登录页面,它没有将我重定向到待办事项列表。

huangapple go评论71阅读模式
英文:

From login page it didnt redirect me to the TO-DO-List

问题

以下是您提供的内容的翻译:

HTML:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>任务清单</title>
  <link rel="stylesheet" href="style.css">
</head>
<body>
  <h1>任务清单</h1>
  <div id="login-container">
    <h2>登录</h2>
    <input type="text" id="username" placeholder="用户名">
    <input type="password" id="password" placeholder="密码">
    <button id="login-btn">登录</button>
  </div>
  <div id="todo-container" style="display: none;">
    <div id="input-container">
      <input type="text" id="new-task" placeholder="输入新任务">
      <button id="add-btn">添加</button>
    </div>
    <ul id="task-list">
    </ul>
  </div>
  <script src="script.js"></script>
</body>
</html>

CSS:

body {
  font-family: Arial, sans-serif;
  background-color: #f2f2f2;
}

h1 {
  text-align: center;
}

#login-container {
  width: 40%;
  margin: 50px auto;
  background-color: #fff;
  padding: 20px;
  border-radius: 5px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
}

#todo-container {
  width: 60%;
  margin: 0 auto;
  background-color: #fff;
  padding: 20px;
  border-radius: 5px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
}

#input-container {
  display: flex;
  justify-content: space between;
  margin-bottom: 20px;
}

#new-task {
  flex-grow: 1;
  padding: 10px;
  font-size: 16px;
  border-radius: 5px;
  border: none;
  box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
}

#add-btn {
  padding: 10px 20px;
  font-size: 16px;
  color: #fff;
  background-color: #007bff;
  border: none;
  border-radius: 5px;
  box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
  cursor: pointer;
}

#task-list {
  list-style: none;
  margin: 0;
  padding: 0;
}

.task {
  margin-bottom: 10px;
  padding: 10px;
  font-size: 16px;
  background-color: #fff;
  border-radius: 5px;
  box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
}

.delete-btn {
  margin-left: 10px;
  padding: 5px 10px;
  font-size: 14px;
  color: #fff;
  background-color: #dc3545;
  border: none;
  border-radius: 5px;
  box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
  cursor: pointer;
}

JS:

const loginForm = document.getElementById("login-container");
const todoContainer = document.getElementById("todo-container");
const usernameInput = document.getElementById("username");
const passwordInput = document.getElementById("password");
const loginBtn = document.getElementById("login-btn");
const addBtn = document.getElementById("add-btn");
const newTaskInput = document.getElementById("new-task");
const taskList = document.getElementById("task-list");

let tasks = [];

// 检查用户是否已登录
if (isLoggedIn()) {
  showTodo();
} else {
  showLogin();
}

// 事件监听器
loginBtn.addEventListener("click", handleLogin);
addBtn.addEventListener("click", handleAddTask);
taskList.addEventListener("click", handleDeleteTask);

// 函数
function handleLogin(event) {
  event.preventDefault();
  const username = usernameInput.value;
  const password = passwordInput.value;
  if (username === "用户名" && password === "密码") {
    showTodo();
    clearLoginInputs();
  } else {
    alert("用户名或密码不正确。请重试。");
  }
}

function handleAddTask(event) {
  event.preventDefault();
  const taskName = newTaskInput.value;
  if (taskName.trim() === "") {
    alert("请输入任务名称。");
    return;
  }
  const newTask = {
    name: taskName,
    completed: false,
  };
  tasks.push(newTask);
  renderTasks();
  clearNewTaskInput();
}

function handleDeleteTask(event) {
  if (event.target.classList.contains("delete-btn")) {
    const taskIndex = event.target.dataset.taskIndex;
    tasks.splice(taskIndex, 1);
    renderTasks();
  }
}

function renderTasks() {
  taskList.innerHTML = "";
  tasks.forEach((task, index) => {
    const taskElement = document.createElement("li");
    taskElement.classList.add("task");
    if (task.completed) {
      taskElement.classList.add("completed");
    }
    const taskNameElement = document.createElement("span");
    taskNameElement.textContent = task.name;
    const deleteBtnElement = document.createElement("button");
    deleteBtnElement.classList.add("delete-btn");
    deleteBtnElement.textContent = "删除";
    deleteBtnElement.setAttribute("data-task-index", index);
    taskElement.appendChild(taskNameElement);
    taskElement.appendChild(deleteBtnElement);
    taskList.appendChild(taskElement);
  });
}

function clearNewTaskInput() {
  newTaskInput.value = "";
}

function clearLoginInputs() {
  usernameInput.value = "";
  passwordInput.value = "";
}

function showLogin() {
  loginForm.style.display = "block";
  todoContainer.style.display = "none";
}

function showTodo() {
  loginForm.style.display = "none";
  todoContainer.style.display = "block";
}

function isLoggedIn() {
  return localStorage.getItem("isLoggedIn") === "true";
}

希望这有所帮助。如果您需要进一步的解释或帮助,请随时告诉我。

英文:

I have here those codes. So basicly this is website with login to page and To do list but when I enter the username and password it didnt redirect me to the to do list website. The password and username in the JS code is password and username for now and it didnt work.Any ideas?

HTML:

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
&lt;meta charset=&quot;UTF-8&quot;&gt;
&lt;title&gt;To-Do List&lt;/title&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;style.css&quot;&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h1&gt;To-Do List&lt;/h1&gt;
&lt;div id=&quot;login-container&quot;&gt;
&lt;h2&gt;Login&lt;/h2&gt;
&lt;input type=&quot;text&quot; id=&quot;username&quot; placeholder=&quot;Username&quot;&gt;
&lt;input type=&quot;password&quot; id=&quot;password&quot; placeholder=&quot;Password&quot;&gt;
&lt;button id=&quot;login-btn&quot;&gt;Login&lt;/button&gt;
&lt;/div&gt;
&lt;div id=&quot;todo-container&quot; style=&quot;display: none;&quot;&gt;
&lt;div id=&quot;input-container&quot;&gt;
&lt;input type=&quot;text&quot; id=&quot;new-task&quot; placeholder=&quot;Enter a new task&quot;&gt;
&lt;button id=&quot;add-btn&quot;&gt;Add&lt;/button&gt;
&lt;/div&gt;
&lt;ul id=&quot;task-list&quot;&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;script src=&quot;script.js&quot;&gt;&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;

CSS:

body {
font-family: Arial, sans-serif;
background-color: #f2f2f2;
}
h1 {
text-align: center;
}
#login-container {
width: 40%;
margin: 50px auto;
background-color: #fff;
padding: 20px;
border-radius: 5px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
}
#todo-container {
width: 60%;
margin: 0 auto;
background-color: #fff;
padding: 20px;
border-radius: 5px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
}
#input-container {
display: flex;
justify-content: space-between;
margin-bottom: 20px;
}
#new-task {
flex-grow: 1;
padding: 10px;
font-size: 16px;
border-radius: 5px;
border: none;
box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
}
#add-btn {
padding: 10px 20px;
font-size: 16px;
color: #fff;
background-color: #007bff;
border: none;
border-radius: 5px;
box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
cursor: pointer;
}
#task-list {
list-style: none;
margin: 0;
padding: 0;
}
.task {
margin-bottom: 10px;
padding: 10px;
font-size: 16px;
background-color: #fff;
border-radius: 5px;
box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
}
.delete-btn {
margin-left: 10px;
padding: 5px 10px;
font-size: 14px;
color: #fff;
background-color: #dc3545;
border: none;
border-radius: 5px;
box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
cursor: pointer;
}

JS

const loginForm = document.getElementById(&quot;login-container&quot;);
const todoContainer = document.getElementById(&quot;todo-container&quot;);
const usernameInput = document.getElementById(&quot;username&quot;);
const passwordInput = document.getElementById(&quot;password&quot;);
const loginBtn = document.getElementById(&quot;login-btn&quot;);
const addBtn = document.getElementById(&quot;add-btn&quot;);
const newTaskInput = document.getElementById(&quot;new-task&quot;);
const taskList = document.getElementById(&quot;task-list&quot;);
let tasks = [];
// Check if the user is logged in
if (isLoggedIn()) {
showTodo();
} else {
showLogin();
}
// Event listeners
loginBtn.addEventListener(&quot;click&quot;, handleLogin);
addBtn.addEventListener(&quot;click&quot;, handleAddTask);
taskList.addEventListener(&quot;click&quot;, handleDeleteTask);
// Functions
function handleLogin(event) {
event.preventDefault();
const username = usernameInput.value;
const password = passwordInput.value;
if (username === &quot;Username&quot; &amp;&amp; password === &quot;password&quot;) {
showTodo();
clearLoginInputs();
} else {
alert(&quot;Incorrect username or password. Please try again.&quot;);
}
}
function handleAddTask(event) {
event.preventDefault();
const taskName = newTaskInput.value;
if (taskName.trim() === &quot;&quot;) {
alert(&quot;Please enter a task name.&quot;);
return;
}
const newTask = {
name: taskName,
completed: false,
};
tasks.push(newTask);
renderTasks();
clearNewTaskInput();
}
function handleDeleteTask(event) {
if (event.target.classList.contains(&quot;delete-btn&quot;)) {
const taskIndex = event.target.dataset.taskIndex;
tasks.splice(taskIndex, 1);
renderTasks();
}
}
function renderTasks() {
taskList.innerHTML = &quot;&quot;;
tasks.forEach((task, index) =&gt; {
const taskElement = document.createElement(&quot;li&quot;);
taskElement.classList.add(&quot;task&quot;);
if (task.completed) {
taskElement.classList.add(&quot;completed&quot;);
}
const taskNameElement = document.createElement(&quot;span&quot;);
taskNameElement.textContent = task.name;
const deleteBtnElement = document.createElement(&quot;button&quot;);
deleteBtnElement.classList.add(&quot;delete-btn&quot;);
deleteBtnElement.textContent = &quot;Delete&quot;;
deleteBtnElement.setAttribute(&quot;data-task-index&quot;, index);
taskElement.appendChild(taskNameElement);
taskElement.appendChild(deleteBtnElement);
taskList.appendChild(taskElement);
});
}
function clearNewTaskInput() {
newTaskInput.value = &quot;&quot;;
}
function clearLoginInputs() {
usernameInput.value = &quot;&quot;;
passwordInput.value = &quot;&quot;;
}
function showLogin() {
loginForm.style.display = &quot;block&quot;;
todoContainer.style.display = &quot;none&quot;;
}
function showTodo() {
loginForm.style.display = &quot;none&quot;;
todoContainer.style.display = &quot;block&quot;;
}
function isLoggedIn() {
return localStorage.getItem(&quot;isLoggedIn&quot;) === &quot;true&quot;;
}

I tryed a lot of things from changing little bit a code to switch to another browser.

答案1

得分: 1

请使用console.log来查找错误的确切位置,而不是发布整个代码。显然不需要css。

尝试这样做:

if (username === "Username" && password === "password") {
    console.log('用户名和密码匹配');
    showTodo();
    clearLoginInputs();
}

如果记录了该语句,这意味着showTodo();clearLoginInputs();被执行。在这种特定情况下,如果登录输入被清除,这意味着该块被调用。

现在的问题是为什么element.style.display="block"不起作用。

查看此链接,我认为它会有所帮助:
https://stackoverflow.com/questions/50460645/getelementbyid-style-display-does-not-work

祝好运 从登录页面,它没有将我重定向到待办事项列表。

英文:

Use console.log to find where exactly the error is instead of posting the entire code. The css was definitely not needed.

Try this:

if (username === &quot;Username&quot; &amp;&amp; password === &quot;password&quot;) {
console.log(&#39;Username and password matched&#39;);
showTodo();
clearLoginInputs();
}

If the statement is logged that would mean showTodo();clearLoginInputs(); are being executed. In this specific case, if the login inputs are being cleared, it means that the block is being called.

Now the question will be why isn't element.style.diplsay="block" not working.

Check out this link. I think it'll help
https://stackoverflow.com/questions/50460645/getelementbyid-style-display-does-not-work

Cheers 从登录页面,它没有将我重定向到待办事项列表。

答案2

得分: 0

我在本地测试了相同的代码,运行正常。你应该检查脚本文件是否被正确地引入。作为演示,可以不使用外部脚本,而是将你的脚本复制粘贴到 .html 文件的 script 标签之间,你会发现它能按预期运行。

英文:

I tested the same code locally and it works fine. You should check that the script file is included correctly. As a demonstration, instead of including an external script, copy and paste your script in the .html file between script tags and you will see that it works as expected.

huangapple
  • 本文由 发表于 2023年2月18日 21:08:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75493544.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定