jquery02.html 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. h1 {
  12. text-align: center;
  13. width: 350px;
  14. height: 70px;
  15. margin: 0 auto;
  16. margin-top: 10px;
  17. background-color: black;
  18. color: white;
  19. font: 36px/70px Arial;
  20. }
  21. #fruits {
  22. width: 350px;
  23. margin: 0 auto;
  24. }
  25. #fruits li {
  26. width: 350px;
  27. height: 75px;
  28. background-color: darkolivegreen;
  29. color: white;
  30. list-style: none;
  31. border-bottom: 1px solid lightgray;
  32. text-align: center;
  33. font: 32px/75px "微软雅黑";
  34. }
  35. #fruits li img {
  36. float: right;
  37. }
  38. #fruits li a:link, #fruits li a:visited {
  39. color: white;
  40. }
  41. #fruits li button {
  42. float: right;
  43. width: 75px;
  44. height: 75px;
  45. border: none;
  46. outline: none;
  47. background-color: coral;
  48. color: white;
  49. }
  50. #fruits li input {
  51. width: 240px;
  52. height: 30px;
  53. text-align: center;
  54. font-size: 22px;
  55. line-height: 30px;
  56. }
  57. #fruits li span {
  58. color: lightgoldenrodyellow;
  59. font-size: 14px;
  60. }
  61. </style>
  62. </head>
  63. <body>
  64. <h1>水果列表</h1>
  65. <ul id="fruits">
  66. <li id="fruit1">苹果<img src="img/icon-trash.png"></li>
  67. <li id="fruit2">香蕉<img src="img/icon-trash.png"></li>
  68. <li id="fruit3">草莓<img src="img/icon-trash.png"></li>
  69. <li>
  70. <input type="text" placeholder="输入水果名称">
  71. <button id="ok">添加</button>
  72. </li>
  73. </ul>
  74. <script src="js/jquery.min.js"></script>
  75. <script>
  76. // jQuery的$函数的用法:
  77. // 1. 如果$函数的参数是一个函数那么这个函数是文档加载完成之后要执行的回调函数
  78. // 2. 如果$函数的参数是选择器字符串那么$函数会返回代表对应的标签的jQuery对象
  79. // 3. 如果$函数的参数是一个原生JavaScript对象那么$函数会返回与之对应的jQuery对象
  80. // 4. 如果$函数的参数一个标签字符串那么$函数会创建对应的元素而且返回jQuery对象
  81. // 原生的JS对象和jQuery对象如何实现转换?
  82. // $(原生JS对象) ---> jQuery对象
  83. // jQuery对象.get(index) / jQuery对象[index] ---> JS对象
  84. // 注: jQuery对象的本质是一个数组
  85. $(function() {
  86. function removeItem(evt) {
  87. $(evt.target).parent()
  88. .css('position', 'relative')
  89. .animate(
  90. {'left': '-350px', 'height': '0'},
  91. 360,
  92. 'linear',
  93. function() {
  94. $(this).remove();
  95. }
  96. );
  97. }
  98. $('#fruits li').hide().each(function(index) {
  99. $(this).delay(600 * index).fadeIn(600);
  100. });
  101. $('#fruits li img').on('click', removeItem);
  102. $('#fruits li:lt(3)').one('click', function(evt) {
  103. var target = $(evt.target);
  104. target.append($('<span>').text(target.attr('id')));
  105. })
  106. var index = 4;
  107. $('#ok').on('click', function() {
  108. var fruit = $('#fruits li input').val().trim();
  109. if (fruit.length > 0) {
  110. $('<li>').text(fruit).attr('id', 'fruit' + index)
  111. .append(
  112. $('<img>').attr('src', 'img/icon-trash.png')
  113. .on('click', removeItem)
  114. )
  115. .one('click', function(evt) {
  116. var target = $(evt.target);
  117. target.append('<span>' + target.attr('id') + '</span>');
  118. })
  119. .insertBefore($('#fruits li:last'))
  120. .hide().fadeIn(600);
  121. $('#fruits li input').val('').focus();
  122. index += 1;
  123. }
  124. });
  125. });
  126. </script>
  127. </body>
  128. </html>