example12.html 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. <style>
  7. #fruits li {
  8. width: 150px;
  9. height: 30px;
  10. background-color: darkolivegreen;
  11. color: white;
  12. list-style: none;
  13. border-bottom: 1px solid lightgray;
  14. text-align: center;
  15. font: 18px/30px "微软雅黑";
  16. }
  17. #fruits li a {
  18. text-decoration: none;
  19. margin-left: 30px;
  20. }
  21. #fruits li a:link, #fruits li a:visited {
  22. color: white;
  23. }
  24. </style>
  25. </head>
  26. <body>
  27. <h1>水果列表</h1>
  28. <hr>
  29. <ul id="fruits">
  30. <li>苹果<a href="javascript:void(0);">x</a></li>
  31. <li>香蕉<a href="javascript:void(0);">x</a></li>
  32. <li>草莓<a href="javascript:void(0);">x</a></li>
  33. </ul>
  34. <input id="fruit" type="text">
  35. <button id="ok">确定</button>
  36. <script src="js/mylib.js"></script>
  37. <script>
  38. function removeItem(evt) {
  39. evt = evt || window.event;
  40. var target = evt.target || evt.srcElement;
  41. $('fruits').removeChild(target.parentNode);
  42. }
  43. +function() {
  44. var aList = document.querySelectorAll('#fruits li a');
  45. for (var i = 0; i < aList.length; i += 1) {
  46. bind(aList[i], 'click', removeItem);
  47. }
  48. bind($('ok'), 'click', function() {
  49. var fruit = $('fruit').value;
  50. if (fruit.trim().length > 0) {
  51. var li = document.createElement('li');
  52. li.textContent = fruit;
  53. var a = document.createElement('a');
  54. a.href = 'javascript:void(0);';
  55. a.textContent = 'x';
  56. bind(a, 'click', removeItem);
  57. li.appendChild(a);
  58. li.style.backgroundColor = 'coral';
  59. li.style.fontFamily = '楷体';
  60. $('fruits').insertBefore(li, $('fruits').firstChild);
  61. $('fruit').value = '';
  62. }
  63. });
  64. }();
  65. </script>
  66. </body>
  67. </html>