为什么当我添加一个新文本时它会添加到所有元素?
$('#btn').on('click', function() {
let newTask = $('#input-text');
if (newTask.val() === '') {
alert('You need to write something');
} else {
let editButton = ('<button class = edit > Edit' + '</button>');
let finishedButton = ('<button class = finished > Finished' + '</button>');
let deleteButton = ('<button class = delete > Delete' + '</button>');
let input = ('<input class= input>');
$('#toDoList').append('<li>' + input + editButton + finishedButton + deleteButton + '</li>');
$('.input').attr('value', newTask.val());
newTask.val('');
}
$('.edit').on('click', function() {
$('.input').prop('disabled', false);
});
$('.finished').on('click', function() {
$(this).parent();
$('#completed').append($(this).parent());
});
$('.delete').on('click', function() {
$(this).parent().fadeOut(500, function() {
$(this).remove();
});
});
});
body {
background-color: ;
}
.container {
display: block;
width: 400px;
margin: 300px auto;
box-shadow: 5px 10px 100px #888888;
min-height: 450px;
max-height: auto;
}
.completed {
margin-top: 3rem;
}
.to-do {
margin-top: 3rem;
}
#btn {
cursor: pointer;
}
.input {
margin-top:
}
.text {
text-align: center;
color: #004690;
}
.color {
color: green;
}
.color1 {
color: red;
}
#input-text {
margin-left: 100px;
}
hr {
width: 50%;
margin-bottom: 1rem;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/style.css">
<meta charset="UTF-8">
<title>todo</title>
</head>
<body>
<div class="container">
<h1 class="text">To Do App</h1>
<hr>
<div class="input">
<input id="input-text" type="text" name="" value="">
<button id="btn">Add</button>
</div>
<div class="to-do">
<h3 class="text color1">TO-do-list</h3>
<hr>
<ul id="toDoList">
慕的地6264312
相关分类