热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

开发笔记:任务列表的简单实现

篇首语:本文由编程笔记#小编为大家整理,主要介绍了任务列表的简单实现相关的知识,希望对你有一定的参考价值。

篇首语:本文由编程笔记#小编为大家整理,主要介绍了任务列表的简单实现相关的知识,希望对你有一定的参考价值。






效果如图
在这里插入图片描述

<div id&#61;"myDIV" class&#61;"header">
<h2 style&#61;"margin:5px">My To Do Listh2>
<input type&#61;"text" id&#61;"myInput" placeholder&#61;"Title...">
<span onclick&#61;"newElement()" class&#61;"addBtn">Addspan>
div>
<ul id&#61;"myUL">
<li>Hit the gymli>
<li class&#61;"checked">Pay billsli>
<li>Meet Georgeli>
<li>Buy eggsli>
<li>Read a bookli>
<li>Organize officeli>
ul>

body {
margin: 0;
min-width: 250px;
}
/* Include the padding and border in an element&#39;s total width and height */
* {
box-sizing: border-box;
}
/* Remove margins and padding from the list */
ul {
margin: 0;
padding: 0;
}
/* Style the list items */
ul li {
cursor: pointer;
position: relative;
padding: 12px 8px 12px 40px;
background: #eee;
font-size: 18px;
transition: 0.2s;

/* make the list items unselectable */
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
/* Set all odd list items to a different color (zebra-stripes) */
ul li:nth-child(odd) {
background: #f9f9f9;
}
/* Darker background-color on hover */
ul li:hover {
background: #ddd;
}
/* When clicked on, add a background color and strike out text */
ul li.checked {
background: #888;
color: #fff;
text-decoration: line-through;
}
/* Add a "checked" mark when clicked on */
ul li.checked::before {
content: &#39;&#39;;
position: absolute;
border-color: #fff;
border-style: solid;
border-width: 0 2px 2px 0;
top: 10px;
left: 16px;
transform: rotate(45deg);
height: 15px;
width: 7px;
}
/* Style the close button */
.close {
position: absolute;
right: 0;
top: 0;
padding: 12px 16px 12px 16px;
}
.close:hover {
background-color: #f44336;
color: white;
}
/* Style the header */
.header {
background-color: pink;
padding: 30px 40px;
color: white;
text-align: center;
}
/* Clear floats after the header */
.header:after {
content: "";
display: table;
clear: both;
}
/* Style the input */
input {
border: none;
width: 75%;
padding: 10px;
float: left;
font-size: 16px;
}
/* Style the "Add" button */
.addBtn {
padding: 10px;
width: 25%;
background: #d9d9d9;
color: #555;
float: left;
text-align: center;
font-size: 16px;
cursor: pointer;
transition: 0.3s;
}
.addBtn:hover {
background-color: #bbb;
}

// Create a "close" button and append it to each list item
var myNodelist &#61; document.getElementsByTagName("LI");
var i;
for (i &#61; 0; i < myNodelist.length; i&#43;&#43;) {
var span &#61; document.createElement("SPAN");
var txt &#61; document.createTextNode("\\u00D7");
span.className &#61; "close";
span.appendChild(txt);
myNodelist[i].appendChild(span);
}
// Click on a close button to hide the current list item
var close &#61; document.getElementsByClassName("close");
var i;
for (i &#61; 0; i < close.length; i&#43;&#43;) {
close[i].onclick &#61; function() {
var div &#61; this.parentElement;
div.style.display &#61; "none";
}
}
// Add a "checked" symbol when clicking on a list item
var list &#61; document.querySelector(&#39;ul&#39;);
list.addEventListener(&#39;click&#39;, function(ev) {
if (ev.target.tagName &#61;&#61;&#61; &#39;LI&#39;) {
ev.target.classList.toggle(&#39;checked&#39;);
}
}, false);
// Create a new list item when clicking on the "Add" button
function newElement() {
var li &#61; document.createElement("li");
var inputValue &#61; document.getElementById("myInput").value;
var t &#61; document.createTextNode(inputValue);
li.appendChild(t);
if (inputValue &#61;&#61;&#61; &#39;&#39;) {
alert("You must write something!");
} else {
document.getElementById("myUL").appendChild(li);
}
document.getElementById("myInput").value &#61; "";
var span &#61; document.createElement("SPAN");
var txt &#61; document.createTextNode("\\u00D7");
span.className &#61; "close";
span.appendChild(txt);
li.appendChild(span);
for (i &#61; 0; i < close.length; i&#43;&#43;) {
close[i].onclick &#61; function() {
var div &#61; this.parentElement;
div.style.display &#61; "none";
}
}
}





推荐阅读
author-avatar
lyw
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有