组件化
//组件开发
//模块与模块之间可以相互依赖,不可以相互影响
//原生js写模块
//生态:文档多,开发者多,回答问题的人多,论坛活跃
var header=(function() {
var a=1;
var b=1;
})();//IIFE
var footer=(function() {
var a = 1;
var b=2;
})()
common.css//自己项目起这两个名字
flobal.css
reset.css//有现成的,不要起与公开、流行库一样、类似的名字,后期不好维护
normalize.css
从右向左找效率高
window.onload=function() {
init();
}
function init() {
// initTodoList;//可以随时维护,好维护,不论写多少模块,都从init中加载
// initB;
}
var initTodoList=(function() {
})();
var initB=(function() {
})();
项目
初步
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://netdna.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<link rel="stylesheet" href="./css/common.css"/>
<!-- //应该闭合,符合w3c规范,不写也能被识别 -->
<link rel="stylesheet" href="./css/index.css">
<title>TodoList</title>
</head>
<body>
<div class="wrap">
<!-- todo上边栏 黑框 -->
<div class="list-hd">
<h2>TodoList</h2>
<a href="javascript:;" class="j-show-input fa fa-plus"></a>
<!-- //fa fa-plus是图标
//能不用id就不用id,class居多,就是js的缩写,后台操作 -->
</div>
//列表部分
<div class="input-wrap">
<div class="input-bd">
<input type="text">
</div>
<div class="btn-bd">
<button>增加项目</button>
</div>
<div class="list-wrap">
<ul class="list"></ul>
</div>
</div>
</div>
<script src="./js/utils.js"></script>
<script src="./js/index1.js"></script>
</body>
</html>
/* 最好有先后顺序,margin在box之前 */
/* 整个todo表 */
.wrap{
position: relative;
width: 500px;
height: 500px;
margin: 50px auto;
box-shadow: 0 0 5px #999;
border-radius: 10px;
overflow: hidden;
}
/* 顺序从右向左 */
/* .wrap .list-hd .a .b .c .d{
} */
/* wrap可以省略 todolist上边栏 黑框*/
.wrap .list-hd{
position: absolute;
top: 0;
left:0;
width: 100%;
height: 44px;
background-color: #000;
color: #fff;
text-align: center;
line-height: 44px;
}
/* 加号的样式 */
.list-hd .fa-plus{
position: absolute;
top: 15px;
right: 15px;
color: #fff;
}
.input-wrap{
display: none;
position: absolute;
top: 44px;
left: 0;
width: 100%;
height: 40px;
border-bottom: 1px solid #ddd;
}
.input-wrap .input-bd{
float: left;
width: 410px;
height: 100%;
/* border: 1px solid #000; */
padding: 5px 5px 5px 10px;
}
.input-wrap .input-bd input{
width: 100%;
height: 100%;
border: 1px solid #ddd;
}
.input-wrap .btn-bd{
float: left;
width: 90px;
height: 100%;
/* border: 1px solid #000; */
padding: 5px 10px 5px 5px ;
}
.input-wrap .btn-bd button{
width: 100%;
height: 100%;
border: 1px solid #ddd;
background-color: #fff;
}
body {
margin: 0;
}
a {
text-decoration: none;
}
ul {
padding: 0;
margin: 0;
list-style: none;
}
h1,
h2,
h3,
h4,
h5,
h6,
p {
margin: 0;
font-weight: normal;
}
div {
box-sizing: border-box;
}
input,
button {
outline: none;
box-sizing: border-box;
border: none;
}
js
没有判断时
init();
function init() {
initTodoList;
}
var initTodoList = (function () {
var plusBtn = document.getElementsByClassName("j-plus-btn")[0],//+号按钮输入框元素
inputArea = document.getElementsByClassName("input-wrap")[0], //整个输入框元素
addItem = document.getElementsByClassName("j-add-item")[0],//list中的增加按钮
itemContent = document.getElementById("itemContent"),
oList = document.getElementsByClassName("item-list")[0],
inputShow = false;
// console.log(plusBtn)
// console.log(inputArea);
// console.log(plusBtn)//通过打印知道哪个函数
addEvent(plusBtn, "click", function () {
console.log(plusBtn);
if (inputShow) {
inputArea.style.display = "none";
inputShow = false;
} else {
inputArea.style.display = "block";
inputShow = true;
}
});
addEvent(addItem, "click", function () {
var content = itemContent.value,//保存输入框的内容
contentLen = content.length,//.消耗性能,每次都。消耗性能 输入字符的长度
oItems = document.getElementsByClassName("item"),//item数组列表
itemLen = oItems.length,//长度
isEdit=false,//是否编辑
curIdx=null;
// item;
if (contentLen <= 0) {//如果输入的值为空则不添加
return;
}
// else {//不用else,因为return了,直接跳出function函数了
// console.log(1)
// }
//如果itemLen大于0,oItems有值,进行判断录入的值是否与已有栏目重合
if (itemLen > 0) {
for (var i = 0; i < itemLen; i++) {
// item=oItems[i];
itemText = elemChildren(oItems[i])[0].innerText;
//得到oItems的第一个子元素p,里面有元素的值,根据tpl方法的模板可知
if (itemText === content) {
alert("已存在该项目");
return; //让整个函数function执行完毕,要不执行下面的
}
}
}
// else {
// console.log(0)
// }
var oLi = document.createElement("li");//创建内容
oLi.className = "item";//是上面找的oItems,是整个item,是整个item的顶端,是item的容器
oLi.innerHTML = itemTpl(content);//把模板的值放入li下面,变成li的子元素
oList.appendChild(oLi);
inputArea.style.display = "none";//隐藏
inputShow = false;
itemContent.value = "";//清零
});
//事件冒泡
addEvent(oList,'click',function(e) {
var e=e||window.event,
tar=e.target||e.srcElement,
liParent=elemParent(tar,2),//删除的是点击元素的父元素,整个item,所以得找到item
className=tar.className,//通过className判断点击的是哪个元素
oItems=document.getElementsByClassName('item'),//就是上面的oLi的集合,数组集合
tarIdx=Array.prototype.indexOf.call(oItems,liParent);//得到第几项,但之后要加1
if (className === 'edit-btn fa fa-edit') {
var itemLen=oItems.length,
item;
inputArea.style.display = "block";
inputShow=true;
for (var i = 0; i < itemLen; i++) {
item=oItems[i];
item.className='item';
}
curIdx=tarIdx;
liParent.className+=' active';
//激活了,添加class,active类上绑定css,可以改变属性,添加类改变属性
// addItem.innerText='编辑第?项';
addItem.innerText='编辑第'+ (curIdx+1)+'项';
}
else if (className === 'remove-btn fa fa-times') {
// elemParent(tar,2).remove();
// liParent.remove();
console.log(liParent)
liParent.remove()
}
});
function itemTpl(text) {
return (
'<p class="item-content">' +
text +
"</p>" +
'<div class="btn-group">' +
'<a href="javascript:;" class="edit-btn fa fa-edit"></a>' +
'<a href="javascript:;" class="remove-btn fa fa-times"></a>' +
"</div>"
);
}
})();
判断时
init();
function init() {
initTodoList;
}
var initTodoList = (function() {
var plusBtn = document.getElementsByClassName('j-plus-btn')[0], //+号按钮输入框
inputArea = document.getElementsByClassName('input-wrap')[0], //整个输入框
addItem = document.getElementsByClassName('j-add-item')[0],
itemContent = document.getElementById('itemContent'),
oList = document.getElementsByClassName('item-list')[0],
inputShow = false,
isEdit = false,
curIdx = null;
console.log(inputArea);
addEvent(plusBtn, 'click', function () {
if (inputShow) {
showInput('close');
restoreStatus();
} else {
showInput('open');
}
});
addEvent(addItem, 'click', function () {
var content = itemContent.value; //保存输入框的内容
contentLen = content.length;
oItems = document.getElementsByClassName('item'),
itemLen = oItems.length;
if (contentLen <= 0) { //如果输入的值为空则不添加
return;
}
if (itemLen > 0) {
for (var i = 0; i < itemLen; i++) {
itemText = elemChildren(oItems[i])[0].innerText;
if (itemText === content) {
alert('已存在该项目');
return;
}
}
}
if (isEdit) {
var oItem = elemChildren(oItems[curIdx])[0];
oItem.innerText = content;
addItem.innerText = '增加项目';
} else {
var oLi = document.createElement('li'); //创建内容
oLi.className = 'item';
oLi.innerHTML = itemTpl(content);
oList.appendChild(oLi);
}
// inputArea.style.display = 'none';
// itemContent.value = '';
// inputShow = false;
showInput('close');
restoreStatus();
});
addEvent(oList, 'click', function (e) {
// console.log(1);
var e = e || window.event,
tar = e.target || e.srcElement,
className = tar.className,
oParent = elemParent(tar, 2),
oItems = document.getElementsByClassName('item');
if (className === 'edit-btn fa fa-edit') {
var itemLen = oItems.length,
tarIdx = Array.prototype.indexOf.call(oItems, oParent),
item;
// inputArea.style.display = 'block';
// inputShow = true;
showInput('open');
for (var i = 0; i < itemLen; i++) {
item = oItems[i];
item.className = 'item';
}
curIdx = tarIdx;
oParent.className += ' active';
addItem.innerText = '编辑第' + (curIdx + 1) + '项';
isEdit = true;
} else if (className === 'remove-btn fa fa-times') {
oParent.remove();
restoreStatus();
}
});
function showInput(action){
if(action === 'close'){
inputArea.style.display = 'none';
inputShow = false;
}else if(action === 'open'){
inputArea.style.display = 'block';
inputShow = true;
}
}
function restoreStatus(){
isEdit = false;
curIdx = null;
itemContent.value = '';
addItem.innerText = '增加项目'
}
function itemTpl(text) {
return (
'<p class="item-content">' + text + '</p>' +
'<div class="btn-group">' +
'<a href="javascript:;" class="edit-btn fa fa-edit"></a>' +
'<a href="javascript:;" class="remove-btn fa fa-times"></a>' +
'</div>'
)
}
})();