example08.html 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. <style>
  7. * {
  8. margin: 0;
  9. padding: 0;
  10. }
  11. #container {
  12. margin: 20px 50px;
  13. }
  14. #fruits li {
  15. list-style: none;
  16. width: 200px;
  17. height: 50px;
  18. font-size: 20px;
  19. line-height: 50px;
  20. background-color: cadetblue;
  21. color: white;
  22. text-align: center;
  23. margin: 2px 0;
  24. }
  25. #fruits>li>a {
  26. float: right;
  27. text-decoration: none;
  28. color: white;
  29. position: relative;
  30. right: 5px;
  31. }
  32. #fruits~input {
  33. border: none;
  34. outline: none;
  35. font-size: 18px;
  36. }
  37. #fruits~input[type=text] {
  38. border-bottom: 1px solid darkgray;
  39. width: 200px;
  40. height: 50px;
  41. text-align: center;
  42. }
  43. #fruits~input[type=button] {
  44. width: 80px;
  45. height: 30px;
  46. background-color: coral;
  47. color: white;
  48. vertical-align: bottom;
  49. cursor: pointer;
  50. }
  51. </style>
  52. </head>
  53. <body>
  54. <div id="container">
  55. <ul id="fruits">
  56. <li>苹果<a href="">×</a></li>
  57. <li>香蕉<a href="">×</a></li>
  58. <li>火龙果<a href="">×</a></li>
  59. <li>西瓜<a href="">×</a></li>
  60. </ul>
  61. <input type="text" name="fruit">
  62. <input id="ok" type="button" value="确定">
  63. </div>
  64. <script src="js/common.js"></script>
  65. <script>
  66. function removeItem(evt) {
  67. evt = evt || window.event;
  68. prevent(evt); // 用自定义函数阻止事件的默认行为
  69. var target = evt.target || evt.srcElement;
  70. var li = target.parentNode;
  71. li.parentNode.removeChild(li);
  72. }
  73. (function() {
  74. function addItem(evt) {
  75. var fruitName = textInput.value.trim();
  76. if (fruitName.length > 0) {
  77. var li = document.createElement("li");
  78. li.textContent = fruitName;
  79. li.style.backgroundColor = "rgba(20, 150, 180, 0.5)";
  80. var a = document.createElement("a");
  81. a.href = "";
  82. a.textContent = "×";
  83. bind(a, "click", removeItem);
  84. li.appendChild(a);
  85. var ul = document.getElementById("fruits");
  86. ul.insertBefore(li, ul.children[0]);
  87. }
  88. textInput.value = "";
  89. textInput.focus();
  90. }
  91. var anchors = document.querySelectorAll("#fruits>li>a");
  92. for (var i = 0; i < anchors.length; i += 1) {
  93. bind(anchors[i], "click", removeItem);
  94. }
  95. var btn = document.getElementById("ok");
  96. var textInput = document.getElementsByName("fruit")[0];
  97. bind(textInput, "keyup", function(evt) {
  98. evt = evt || window.event;
  99. var code = evt.keyCode || evt.which;
  100. // console.log(code);
  101. if (code == 13) {
  102. addItem();
  103. }
  104. });
  105. bind(btn, "click", addItem);
  106. })();
  107. </script>
  108. </body>
  109. </html>