组件化

  1. //组件开发
  2. //模块与模块之间可以相互依赖,不可以相互影响
  3. //原生js写模块
  4. //生态:文档多,开发者多,回答问题的人多,论坛活跃
  5. var header=(function() {
  6. var a=1;
  7. var b=1;
  8. })();//IIFE
  9. var footer=(function() {
  10. var a = 1;
  11. var b=2;
  12. })()
  13. common.css//自己项目起这两个名字
  14. flobal.css
  15. reset.css//有现成的,不要起与公开、流行库一样、类似的名字,后期不好维护
  16. normalize.css

image.png
从右向左找效率高

  1. window.onload=function() {
  2. init();
  3. }
  4. function init() {
  5. // initTodoList;//可以随时维护,好维护,不论写多少模块,都从init中加载
  6. // initB;
  7. }
  8. var initTodoList=(function() {
  9. })();
  10. var initB=(function() {
  11. })();

项目

初步

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <link href="https://netdna.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
  8. <link rel="stylesheet" href="./css/common.css"/>
  9. <!-- //应该闭合,符合w3c规范,不写也能被识别 -->
  10. <link rel="stylesheet" href="./css/index.css">
  11. <title>TodoList</title>
  12. </head>
  13. <body>
  14. <div class="wrap">
  15. <!-- todo上边栏 黑框 -->
  16. <div class="list-hd">
  17. <h2>TodoList</h2>
  18. <a href="javascript:;" class="j-show-input fa fa-plus"></a>
  19. <!-- //fa fa-plus是图标
  20. //能不用id就不用id,class居多,就是js的缩写,后台操作 -->
  21. </div>
  22. //列表部分
  23. <div class="input-wrap">
  24. <div class="input-bd">
  25. <input type="text">
  26. </div>
  27. <div class="btn-bd">
  28. <button>增加项目</button>
  29. </div>
  30. <div class="list-wrap">
  31. <ul class="list"></ul>
  32. </div>
  33. </div>
  34. </div>
  35. <script src="./js/utils.js"></script>
  36. <script src="./js/index1.js"></script>
  37. </body>
  38. </html>
  1. /* 最好有先后顺序,margin在box之前 */
  2. /* 整个todo表 */
  3. .wrap{
  4. position: relative;
  5. width: 500px;
  6. height: 500px;
  7. margin: 50px auto;
  8. box-shadow: 0 0 5px #999;
  9. border-radius: 10px;
  10. overflow: hidden;
  11. }
  12. /* 顺序从右向左 */
  13. /* .wrap .list-hd .a .b .c .d{
  14. } */
  15. /* wrap可以省略 todolist上边栏 黑框*/
  16. .wrap .list-hd{
  17. position: absolute;
  18. top: 0;
  19. left:0;
  20. width: 100%;
  21. height: 44px;
  22. background-color: #000;
  23. color: #fff;
  24. text-align: center;
  25. line-height: 44px;
  26. }
  27. /* 加号的样式 */
  28. .list-hd .fa-plus{
  29. position: absolute;
  30. top: 15px;
  31. right: 15px;
  32. color: #fff;
  33. }
  34. .input-wrap{
  35. display: none;
  36. position: absolute;
  37. top: 44px;
  38. left: 0;
  39. width: 100%;
  40. height: 40px;
  41. border-bottom: 1px solid #ddd;
  42. }
  43. .input-wrap .input-bd{
  44. float: left;
  45. width: 410px;
  46. height: 100%;
  47. /* border: 1px solid #000; */
  48. padding: 5px 5px 5px 10px;
  49. }
  50. .input-wrap .input-bd input{
  51. width: 100%;
  52. height: 100%;
  53. border: 1px solid #ddd;
  54. }
  55. .input-wrap .btn-bd{
  56. float: left;
  57. width: 90px;
  58. height: 100%;
  59. /* border: 1px solid #000; */
  60. padding: 5px 10px 5px 5px ;
  61. }
  62. .input-wrap .btn-bd button{
  63. width: 100%;
  64. height: 100%;
  65. border: 1px solid #ddd;
  66. background-color: #fff;
  67. }
  1. body {
  2. margin: 0;
  3. }
  4. a {
  5. text-decoration: none;
  6. }
  7. ul {
  8. padding: 0;
  9. margin: 0;
  10. list-style: none;
  11. }
  12. h1,
  13. h2,
  14. h3,
  15. h4,
  16. h5,
  17. h6,
  18. p {
  19. margin: 0;
  20. font-weight: normal;
  21. }
  22. div {
  23. box-sizing: border-box;
  24. }
  25. input,
  26. button {
  27. outline: none;
  28. box-sizing: border-box;
  29. border: none;
  30. }

js

没有判断时

  1. init();
  2. function init() {
  3. initTodoList;
  4. }
  5. var initTodoList = (function () {
  6. var plusBtn = document.getElementsByClassName("j-plus-btn")[0],//+号按钮输入框元素
  7. inputArea = document.getElementsByClassName("input-wrap")[0], //整个输入框元素
  8. addItem = document.getElementsByClassName("j-add-item")[0],//list中的增加按钮
  9. itemContent = document.getElementById("itemContent"),
  10. oList = document.getElementsByClassName("item-list")[0],
  11. inputShow = false;
  12. // console.log(plusBtn)
  13. // console.log(inputArea);
  14. // console.log(plusBtn)//通过打印知道哪个函数
  15. addEvent(plusBtn, "click", function () {
  16. console.log(plusBtn);
  17. if (inputShow) {
  18. inputArea.style.display = "none";
  19. inputShow = false;
  20. } else {
  21. inputArea.style.display = "block";
  22. inputShow = true;
  23. }
  24. });
  25. addEvent(addItem, "click", function () {
  26. var content = itemContent.value,//保存输入框的内容
  27. contentLen = content.length,//.消耗性能,每次都。消耗性能 输入字符的长度
  28. oItems = document.getElementsByClassName("item"),//item数组列表
  29. itemLen = oItems.length,//长度
  30. isEdit=false,//是否编辑
  31. curIdx=null;
  32. // item;
  33. if (contentLen <= 0) {//如果输入的值为空则不添加
  34. return;
  35. }
  36. // else {//不用else,因为return了,直接跳出function函数了
  37. // console.log(1)
  38. // }
  39. //如果itemLen大于0,oItems有值,进行判断录入的值是否与已有栏目重合
  40. if (itemLen > 0) {
  41. for (var i = 0; i < itemLen; i++) {
  42. // item=oItems[i];
  43. itemText = elemChildren(oItems[i])[0].innerText;
  44. //得到oItems的第一个子元素p,里面有元素的值,根据tpl方法的模板可知
  45. if (itemText === content) {
  46. alert("已存在该项目");
  47. return; //让整个函数function执行完毕,要不执行下面的
  48. }
  49. }
  50. }
  51. // else {
  52. // console.log(0)
  53. // }
  54. var oLi = document.createElement("li");//创建内容
  55. oLi.className = "item";//是上面找的oItems,是整个item,是整个item的顶端,是item的容器
  56. oLi.innerHTML = itemTpl(content);//把模板的值放入li下面,变成li的子元素
  57. oList.appendChild(oLi);
  58. inputArea.style.display = "none";//隐藏
  59. inputShow = false;
  60. itemContent.value = "";//清零
  61. });
  62. //事件冒泡
  63. addEvent(oList,'click',function(e) {
  64. var e=e||window.event,
  65. tar=e.target||e.srcElement,
  66. liParent=elemParent(tar,2),//删除的是点击元素的父元素,整个item,所以得找到item
  67. className=tar.className,//通过className判断点击的是哪个元素
  68. oItems=document.getElementsByClassName('item'),//就是上面的oLi的集合,数组集合
  69. tarIdx=Array.prototype.indexOf.call(oItems,liParent);//得到第几项,但之后要加1
  70. if (className === 'edit-btn fa fa-edit') {
  71. var itemLen=oItems.length,
  72. item;
  73. inputArea.style.display = "block";
  74. inputShow=true;
  75. for (var i = 0; i < itemLen; i++) {
  76. item=oItems[i];
  77. item.className='item';
  78. }
  79. curIdx=tarIdx;
  80. liParent.className+=' active';
  81. //激活了,添加class,active类上绑定css,可以改变属性,添加类改变属性
  82. // addItem.innerText='编辑第?项';
  83. addItem.innerText='编辑第'+ (curIdx+1)+'项';
  84. }
  85. else if (className === 'remove-btn fa fa-times') {
  86. // elemParent(tar,2).remove();
  87. // liParent.remove();
  88. console.log(liParent)
  89. liParent.remove()
  90. }
  91. });
  92. function itemTpl(text) {
  93. return (
  94. '<p class="item-content">' +
  95. text +
  96. "</p>" +
  97. '<div class="btn-group">' +
  98. '<a href="javascript:;" class="edit-btn fa fa-edit"></a>' +
  99. '<a href="javascript:;" class="remove-btn fa fa-times"></a>' +
  100. "</div>"
  101. );
  102. }
  103. })();

判断时

  1. init();
  2. function init() {
  3. initTodoList;
  4. }
  5. var initTodoList = (function() {
  6. var plusBtn = document.getElementsByClassName('j-plus-btn')[0], //+号按钮输入框
  7. inputArea = document.getElementsByClassName('input-wrap')[0], //整个输入框
  8. addItem = document.getElementsByClassName('j-add-item')[0],
  9. itemContent = document.getElementById('itemContent'),
  10. oList = document.getElementsByClassName('item-list')[0],
  11. inputShow = false,
  12. isEdit = false,
  13. curIdx = null;
  14. console.log(inputArea);
  15. addEvent(plusBtn, 'click', function () {
  16. if (inputShow) {
  17. showInput('close');
  18. restoreStatus();
  19. } else {
  20. showInput('open');
  21. }
  22. });
  23. addEvent(addItem, 'click', function () {
  24. var content = itemContent.value; //保存输入框的内容
  25. contentLen = content.length;
  26. oItems = document.getElementsByClassName('item'),
  27. itemLen = oItems.length;
  28. if (contentLen <= 0) { //如果输入的值为空则不添加
  29. return;
  30. }
  31. if (itemLen > 0) {
  32. for (var i = 0; i < itemLen; i++) {
  33. itemText = elemChildren(oItems[i])[0].innerText;
  34. if (itemText === content) {
  35. alert('已存在该项目');
  36. return;
  37. }
  38. }
  39. }
  40. if (isEdit) {
  41. var oItem = elemChildren(oItems[curIdx])[0];
  42. oItem.innerText = content;
  43. addItem.innerText = '增加项目';
  44. } else {
  45. var oLi = document.createElement('li'); //创建内容
  46. oLi.className = 'item';
  47. oLi.innerHTML = itemTpl(content);
  48. oList.appendChild(oLi);
  49. }
  50. // inputArea.style.display = 'none';
  51. // itemContent.value = '';
  52. // inputShow = false;
  53. showInput('close');
  54. restoreStatus();
  55. });
  56. addEvent(oList, 'click', function (e) {
  57. // console.log(1);
  58. var e = e || window.event,
  59. tar = e.target || e.srcElement,
  60. className = tar.className,
  61. oParent = elemParent(tar, 2),
  62. oItems = document.getElementsByClassName('item');
  63. if (className === 'edit-btn fa fa-edit') {
  64. var itemLen = oItems.length,
  65. tarIdx = Array.prototype.indexOf.call(oItems, oParent),
  66. item;
  67. // inputArea.style.display = 'block';
  68. // inputShow = true;
  69. showInput('open');
  70. for (var i = 0; i < itemLen; i++) {
  71. item = oItems[i];
  72. item.className = 'item';
  73. }
  74. curIdx = tarIdx;
  75. oParent.className += ' active';
  76. addItem.innerText = '编辑第' + (curIdx + 1) + '项';
  77. isEdit = true;
  78. } else if (className === 'remove-btn fa fa-times') {
  79. oParent.remove();
  80. restoreStatus();
  81. }
  82. });
  83. function showInput(action){
  84. if(action === 'close'){
  85. inputArea.style.display = 'none';
  86. inputShow = false;
  87. }else if(action === 'open'){
  88. inputArea.style.display = 'block';
  89. inputShow = true;
  90. }
  91. }
  92. function restoreStatus(){
  93. isEdit = false;
  94. curIdx = null;
  95. itemContent.value = '';
  96. addItem.innerText = '增加项目'
  97. }
  98. function itemTpl(text) {
  99. return (
  100. '<p class="item-content">' + text + '</p>' +
  101. '<div class="btn-group">' +
  102. '<a href="javascript:;" class="edit-btn fa fa-edit"></a>' +
  103. '<a href="javascript:;" class="remove-btn fa fa-times"></a>' +
  104. '</div>'
  105. )
  106. }
  107. })();