[英]How can i edit the Li item to add text to it with javascript
我正在创建一个类似trello的网络应用进行练习,所以我已经完成了具有拖放功能的卡片,但是现在我想向ul标签的li项添加内容,甚至用户也可以对其进行编辑
我希望它在javascript中
HTML代码
<!DOCTYPE html>
<html lang="en">
<head>
<title>Trello</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="/css/style.css" rel="stylesheet">
</head>
<body>
<!-- Masthead -->
<header class="masthead">
<div class="boards-menu">
<div class="board-search">
<input type="search" class="board-search-input" aria-label="Board Search">
<i class="fas fa-search search-icon" aria-hidden="true"></i>
</div>
</div>
<div class="logo">
<h1><i class="fab fa-trello logo-icon" aria-hidden="true"></i>Trello</h1>
</div>
<div class="user-settings" style="height: 20px;margin-bottom:35px;">
<a href="profile.html">
<img src="https://img.icons8.com/wired/64/000000/settings.png">
</a>
</div>
</header>
<!-- Lists container -->
<section class="lists-container">
<div class="list" id="dragabble_card">
<h3 class="list-title">Tasks to Do</h3>
<ul class="list-items" id="parent">
<li id="repeatTHIS"> </li>
</ul>
<button class="add-card-btn btn" id="button" onclick="repeat()">Add a card</button>
</div>
<div class="list" id="dragabble_card1">
<h3 class="list-title">Completed Tasks</h3>
<ul class="list-items" id="parent">
<li id="repeatTHIS"> </li>
</ul>
<button class="add-card-btn btn" id="button" onclick="repeat()">Add a card</button>
</div>
<div class="list" id="dragabble_card">
<h3 class="list-title">Tasks to Do</h3>
<ul class="list-items" id="parent">
<li id="repeatTHIS"> </li>
</ul>
<button class="add-card-btn btn" id="button" onclick="repeat()">Add a card</button>
</div>
<button class="add-list-btn btn">Add a list</button>
</section>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script src="script/script.js">
</script>
</body>
</html>
使用Javascript
$(function () {
$("#parent, #parent1").sortable({
connectWith: ".list-items"
}).disableSelection();
});
document.getElementById('button').onclick = repeat;
var i = 0;
var original = document.getElementById('repeatTHIS');
function repeat() {
var clone = original.cloneNode(true);
clone.id = "repeatTHIS" + ++i;
original.parentNode.appendChild(clone);
}
我的CSS如下
body {
font-family: Arial, sans-serif;
display: flex;
flex-direction: column;
}
.btn {
display: flex;
justify-content: center;
align-items: center;
font: inherit;
background: none;
border: none;
color: inherit;
padding: 0;
cursor: pointer;
}
:focus {
outline-color: #fa0;
}
:root {
font-size: 11px;
}
*,
*::before,
*::after {
box-sizing: border-box;
}
/* Masthead */
.masthead {
flex-basis: 4rem;
display: flex;
align-items: center;
padding: 0 0.8rem;
margin: -8px -8px 0 -8px;
background-color: #0067a3;
box-shadow: 0 0.1rem 0.1rem rgba(0, 0, 0, 0.1);
}
.masthead .btn {
background-color: #4c94be;
border-radius: 0.3rem;
transition: background-color 150ms;
}
.masthead .btn:hover {
background-color: #3385b5;
}
.boards-menu {
display: flex;
flex-shrink: 0;
}
.boards-btn {
flex-basis: 9rem;
font-size: 1.4rem;
font-weight: 700;
color: #fff;
margin-right: 0.8rem;
padding: 0.6rem 0.8rem;
}
.boards-btn-icon {
font-size: 1.7rem;
padding-right: 1.2rem;
}
.board-search {
flex-basis: 18rem;
position: relative;
}
.board-search-input {
height: 3rem;
border: none;
border-radius: 0.3rem;
background-color: #4c94be;
width: 100%;
padding: 0 3rem 0 1rem;
color: #fff;
}
.board-search-input:hover {
background-color: #66a4c8;
}
.search-icon {
font-size: 1.5rem;
position: absolute;
top: 50%;
right: 0.8rem;
transform: translateY(-50%) rotate(90deg);
color: #fff;
}
.logo {
flex: 1;
font-family: "Courgette", cursive;
font-size: 2.2rem;
font-weight: 700;
color: rgba(255, 255, 255, 0.5);
margin: 0 2rem;
transition: color 150ms;
text-align: center;
white-space: nowrap;
cursor: pointer;
}
.logo:hover {
color: rgba(255, 255, 255, 0.8);
}
.logo-icon {
padding-right: 0.4rem;
}
.user-settings {
display: flex;
height: 1.5rem;
color: #fff;
}
.user-settings-btn {
font-size: 1.5rem;
width: 3.5rem;
margin-right: 1.5rem;
}
/* Board info bar */
.board-info-bar {
flex-basis: 3rem;
display: flex;
align-items: center;
justify-content: space-between;
margin: 0.8rem 0;
padding: 0 1rem;
color: #f6f6f6;
}
sboard-info-bar .btn {
font-size: 1.4rem;
font-weight: 400;
transition: background-color 150ms;
padding: 0 0.6rem;
border-radius: 0.3rem;
height: 3rem;
}
.board-info-bar .btn:hover {
background-color: #006aa8;
}
.private-btn-icon,
.menu-btn-icon {
padding-right: 0.6rem;
white-space: nowrap;
}
.board-title h2 {
font-size: 1.8rem;
font-weight: 700;
white-space: nowrap;
}
/* Lists */
.lists-container::-webkit-scrollbar {
height: 2.4rem;
}
.lists-container::-webkit-scrollbar-thumb {
background-color: #66a3c7;
border: 0.8rem solid #0079bf;
border-top-width: 0;
}
.lists-container {
display: flex;
align-items: start;
padding: 0 0.8rem 0.8rem;
overflow-x: auto;
height: calc(100vh - 8.6rem);
margin-top: 60px;
}
.list {
flex: 0 0 27rem;
display: flex;
flex-direction: column;
background-color: #e2e4e6;
max-height: calc(100vh - 11.8rem);
border-radius: 0.3rem;
margin-right: 1rem;
}
.list:last-of-type {
margin-right: 0;
}
.list-title {
font-size: 1.4rem;
font-weight: 700;
color: #333;
padding: 1rem;
}
.list-items {
flex: 1;
display: flex;
flex-direction: column;
align-content: start;
padding: 0 0.6rem 0.5rem;
overflow-y: auto;
}
.list-items::-webkit-scrollbar {
width: 1.6rem;
}
.list-items::-webkit-scrollbar-thumb {
background-color: #c4c9cc;
border-right: 0.6rem solid #e2e4e6;
}
.list-items li {
font-size: 1.4rem;
font-weight: 400;
line-height: 1.3;
background-color: #fff;
padding: 0.65rem 0.6rem;
color: #4d4d4d;
border-bottom: 0.1rem solid #ccc;
border-radius: 0.3rem;
margin-bottom: 0.6rem;
word-wrap: break-word;
cursor: pointer;
}
.list-items li:last-of-type {
margin-bottom: 0;
}
.list-items li:hover {
background-color: #eee;
}
.add-card-btn {
display: block;
font-size: 1.4rem;
font-weight: 400;
color: #838c91;
padding: 1rem;
text-align: left;
cursor: pointer;
}
.add-card-btn:hover {
background-color: #cdd2d4;
color: #4d4d4d;
text-decoration: underline;
}
.add-list-btn {
flex: 0 0 27rem;
display: block;
font-size: 1.4rem;
font-weight: 400;
background-color: #006aa7;
color: #a5cae0;
padding: 1rem;
border-radius: 0.3rem;
cursor: pointer;
transition: background-color 150ms;
text-align: left;
margin-left: 15px;
}
.add-list-btn:hover {
background-color: #005485;
}
.add-card-btn::after,
.add-list-btn::after {
content: '...';
}
$(function () {
$("#parent, #parent1").sortable({
connectWith: ".list-items"
}).disableSelection();
});
document.getElementById('button').onclick = repeat;
var i = 0;
var original = document.getElementById('repeatTHIS');
function repeat() {
var clone = original.cloneNode(true);
clone.id = "repeatTHIS" + ++i;
original.parentNode.appendChild(clone);
}
body {
font-family: Arial, sans-serif;
display: flex;
flex-direction: column;
}
.btn {
display: flex;
justify-content: center;
align-items: center;
font: inherit;
background: none;
border: none;
color: inherit;
padding: 0;
cursor: pointer;
}
:focus {
outline-color: #fa0;
}
:root {
font-size: 11px;
}
*,
*::before,
*::after {
box-sizing: border-box;
}
/* Masthead */
.masthead {
flex-basis: 4rem;
display: flex;
align-items: center;
padding: 0 0.8rem;
margin: -8px -8px 0 -8px;
background-color: #0067a3;
box-shadow: 0 0.1rem 0.1rem rgba(0, 0, 0, 0.1);
}
.masthead .btn {
background-color: #4c94be;
border-radius: 0.3rem;
transition: background-color 150ms;
}
.masthead .btn:hover {
background-color: #3385b5;
}
.boards-menu {
display: flex;
flex-shrink: 0;
}
.boards-btn {
flex-basis: 9rem;
font-size: 1.4rem;
font-weight: 700;
color: #fff;
margin-right: 0.8rem;
padding: 0.6rem 0.8rem;
}
.boards-btn-icon {
font-size: 1.7rem;
padding-right: 1.2rem;
}
.board-search {
flex-basis: 18rem;
position: relative;
}
.board-search-input {
height: 3rem;
border: none;
border-radius: 0.3rem;
background-color: #4c94be;
width: 100%;
padding: 0 3rem 0 1rem;
color: #fff;
}
.board-search-input:hover {
background-color: #66a4c8;
}
.search-icon {
font-size: 1.5rem;
position: absolute;
top: 50%;
right: 0.8rem;
transform: translateY(-50%) rotate(90deg);
color: #fff;
}
.logo {
flex: 1;
font-family: "Courgette", cursive;
font-size: 2.2rem;
font-weight: 700;
color: rgba(255, 255, 255, 0.5);
margin: 0 2rem;
transition: color 150ms;
text-align: center;
white-space: nowrap;
cursor: pointer;
}
.logo:hover {
color: rgba(255, 255, 255, 0.8);
}
.logo-icon {
padding-right: 0.4rem;
}
.user-settings {
display: flex;
height: 1.5rem;
color: #fff;
}
.user-settings-btn {<!DOCTYPE html>
<html lang="en">
<head>
<title>Trello</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="/css/style.css" rel="stylesheet">
</head>
<body>
<!-- Masthead -->
<header class="masthead">
<div class="boards-menu">
<div class="board-search">
<input type="search" class="board-search-input" aria-label="Board Search">
<i class="fas fa-search search-icon" aria-hidden="true"></i>
</div>
</div>
<div class="logo">
<h1><i class="fab fa-trello logo-icon" aria-hidden="true"></i>Trello</h1>
</div>
<div class="user-settings" style="height: 20px;margin-bottom:35px;">
<a href="profile.html">
<img src="https://img.icons8.com/wired/64/000000/settings.png">
</a>
</div>
</header>
<!-- Lists container -->
<section class="lists-container">
<div class="list" id="dragabble_card">
<h3 class="list-title">Tasks to Do</h3>
<ul class="list-items" id="parent">
<li id="repeatTHIS"> </li>
</ul>
<button class="add-card-btn btn" id="button" onclick="repeat()">Add a card</button>
</div>
<div class="list" id="dragabble_card1">
<h3 class="list-title">Completed Tasks</h3>
<ul class="list-items" id="parent">
<li id="repeatTHIS"> </li>
</ul>
<button class="add-card-btn btn" id="button" onclick="repeat()">Add a card</button>
</div>
<div class="list" id="dragabble_card">
<h3 class="list-title">Tasks to Do</h3>
<ul class="list-items" id="parent">
<li id="repeatTHIS"> </li>
</ul>
<button class="add-card-btn btn" id="button" onclick="repeat()">Add a card</button>
</div>
<button class="add-list-btn btn">Add a list</button>
</section>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script src="script/script.js">
</script>
</body>
</html>
font-size: 1.5rem;
width: 3.5rem;
margin-right: 1.5rem;
}
/* Board info bar */
.board-info-bar {
flex-basis: 3rem;
display: flex;
align-items: center;
justify-content: space-between;
margin: 0.8rem 0;
padding: 0 1rem;
color: #f6f6f6;
}
sboard-info-bar .btn {
font-size: 1.4rem;
font-weight: 400;
transition: background-color 150ms;
padding: 0 0.6rem;
border-radius: 0.3rem;
height: 3rem;
}
.board-info-bar .btn:hover {
background-color: #006aa8;
}
.private-btn-icon,
.menu-btn-icon {
padding-right: 0.6rem;
white-space: nowrap;
}
.board-title h2 {
font-size: 1.8rem;
font-weight: 700;
white-space: nowrap;
}
/* Lists */
.lists-container::-webkit-scrollbar {
height: 2.4rem;
}
.lists-container::-webkit-scrollbar-thumb {
background-color: #66a3c7;
border: 0.8rem solid #0079bf;
border-top-width: 0;
}
.lists-container {
display: flex;
align-items: start;
padding: 0 0.8rem 0.8rem;
overflow-x: auto;
height: calc(100vh - 8.6rem);
margin-top: 60px;
}
.list {
flex: 0 0 27rem;
display: flex;
flex-direction: column;
background-color: #e2e4e6;
max-height: calc(100vh - 11.8rem);
border-radius: 0.3rem;
margin-right: 1rem;
}
.list:last-of-type {
margin-right: 0;
}
.list-title {
font-size: 1.4rem;
font-weight: 700;
color: #333;
padding: 1rem;
}
.list-items {
flex: 1;
display: flex;
flex-direction: column;
align-content: start;
padding: 0 0.6rem 0.5rem;
overflow-y: auto;
}
.list-items::-webkit-scrollbar {
width: 1.6rem;
}
.list-items::-webkit-scrollbar-thumb {
background-color: #c4c9cc;
border-right: 0.6rem solid #e2e4e6;
}
.list-items li {
font-size: 1.4rem;
font-weight: 400;
line-height: 1.3;
background-color: #fff;
padding: 0.65rem 0.6rem;
color: #4d4d4d;
border-bottom: 0.1rem solid #ccc;
border-radius: 0.3rem;
margin-bottom: 0.6rem;
word-wrap: break-word;
cursor: pointer;
}
.list-items li:last-of-type {
margin-bottom: 0;
}
.list-items li:hover {
background-color: #eee;
}
.add-card-btn {
display: block;
font-size: 1.4rem;
font-weight: 400;
color: #838c91;
padding: 1rem;
text-align: left;
cursor: pointer;
}
.add-card-btn:hover {
background-color: #cdd2d4;
color: #4d4d4d;
text-decoration: underline;
}
.add-list-btn {
flex: 0 0 27rem;
display: block;
font-size: 1.4rem;
font-weight: 400;
background-color: #006aa7;
color: #a5cae0;
padding: 1rem;
border-radius: 0.3rem;
cursor: pointer;
transition: background-color 150ms;
text-align: left;
margin-left: 15px;
}
.add-list-btn:hover {
background-color: #005485;
}
.add-card-btn::after,
.add-list-btn::after {
content: '...';
}
<!DOCTYPE html>
<html lang="en">
<head>
<title>Trello</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="/css/style.css" rel="stylesheet">
</head>
<body>
<!-- Masthead -->
<header class="masthead">
<div class="boards-menu">
<div class="board-search">
<input type="search" class="board-search-input" aria-label="Board Search">
<i class="fas fa-search search-icon" aria-hidden="true"></i>
</div>
</div>
<div class="logo">
<h1><i class="fab fa-trello logo-icon" aria-hidden="true"></i>Trello</h1>
</div>
<div class="user-settings" style="height: 20px;margin-bottom:35px;">
<a href="profile.html">
<img src="https://img.icons8.com/wired/64/000000/settings.png">
</a>
</div>
</header>
<!-- Lists container -->
<section class="lists-container">
<div class="list" id="dragabble_card">
<h3 class="list-title">Tasks to Do</h3>
<ul class="list-items" id="parent">
<li id="repeatTHIS"> </li>
</ul>
<button class="add-card-btn btn" id="button" onclick="repeat()">Add a card</button>
</div>
<div class="list" id="dragabble_card1">
<h3 class="list-title">Completed Tasks</h3>
<ul class="list-items" id="parent">
<li id="repeatTHIS"> </li>
</ul>
<button class="add-card-btn btn" id="button" onclick="repeat()">Add a card</button>
</div>
<div class="list" id="dragabble_card">
<h3 class="list-title">Tasks to Do</h3>
<ul class="list-items" id="parent">
<li id="repeatTHIS"> </li>
</ul>
<button class="add-card-btn btn" id="button" onclick="repeat()">Add a card</button>
</div>
<button class="add-list-btn btn">Add a list</button>
</section>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script src="script/script.js">
</script>
</body>
</html>
您可以使用称为contenteditable
的HTML 属性 ,这将允许用户编辑元素上的html内容! 但是,如果需要保存更改,则必须在PHP
页面中添加一些javascript AJAX
请求,这可能会将更改保存到数据库或文件或其他文件中。 希望我的回答对您有用!
window.isEditingALi=false; document.addEventListener("click", function(e){ //Of course you can merge this EventListener with the next one but I am separing them just to make things clear for you ! const elementsIdSelector="editableLi"; e=(e||window.event); e.preventDefault(); const path=e.path; for(var i=0;i<path.length-4;i++){ if(path[i].tagName=="LI"&&path[i].id==elementsIdSelector){ //Found a Li element with the id required ( Even dynamically created li would fires ! ) //Do whatever you want there path[i].addEventListener("click", function(){ if(this.getAttribute("contenteditable")!="true"){ const wantEdit=window.confirm("You want to edit this element content ?"); if(wantEdit){ this.setAttribute("contenteditable", true); window.isEditingALi=this; this.focus(); }else{ window.isEditingALi=false; } } }); } } }); document.addEventListener("click", function(e){ e=(e||window.event); e.preventDefault(); const path=e.path; let canGetReset=true; for(var i=0;i<path.length-4;i++){ if(path[i]==window.isEditingALi) canGetReset=false; } if(canGetReset&&window.isEditingALi){ window.isEditingALi.removeAttribute("contenteditable"); //Here you can add your Ajax request or whatever function you want to do after the user finish editing the li .. alert("Li has been changed successfully !"); window.isEditingALi=false; } }); //This is just an additional function to test if the dynamically created element would work or not ! function createEditableLi(){ const ul=document.querySelector("ul"); const newLi=document.createElement("li"); newLi.setAttribute("id", "editableLi"); newLi.innerHTML="New Li (Created dynamically)"; ul.appendChild(newLi); }
body{ font-family: Arial, sans-serif; font-size: 14px; }
You can actually edit these Li by clicking and apply any changes you want ! (Even dynamically created elements are supported now!) <br> <ul> <li id="editableLi"> First Li</li> <li id="editableLi"> Second Li</li> <li id="editableLi"> Third Li</li> <li id="editableLi"> Another Li</li> <li id="editableLi"> More Li</li> </ul> <br> <button onclick="createEditableLi()">Create a New Li Dynamically</button>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.