You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

list_by_jquery.html 2.9 kB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>动态列表</title>
  6. <style>
  7. * {
  8. margin: 0;
  9. padding: 0;
  10. }
  11. body {
  12. background-color: #000;
  13. color: #fff;
  14. }
  15. #app {
  16. width: 40%;
  17. margin: 20px auto;
  18. }
  19. #fruits>li {
  20. width: 90%;
  21. height: 50px;
  22. background-color: #6ca;
  23. margin: 4px 0;
  24. text-align: center;
  25. font-size: 20px;
  26. list-style-type: none;
  27. line-height: 50px;
  28. }
  29. #fruits>li>a {
  30. float: right;
  31. color: #fff;
  32. text-decoration: none;
  33. margin-right: 10px;
  34. }
  35. #fruits+div {
  36. margin-top: 20px;
  37. }
  38. #fname {
  39. width: 70%;
  40. height: 40px;
  41. color: #fff;
  42. border-radius: 8px;
  43. border: none;
  44. outline: none;
  45. font-size: 20px;
  46. text-align: center;
  47. vertical-align: middle;
  48. background-color: #999;
  49. }
  50. #ok {
  51. width: 19%;
  52. height: 40px;
  53. color: #fff;
  54. background-color: #a45;
  55. border: none;
  56. outline: none;
  57. font-size: 16px;
  58. vertical-align: middle;
  59. }
  60. </style>
  61. </head>
  62. <body>
  63. <div id="app">
  64. <ul id="fruits">
  65. <li>苹果<a href="">×</a></li>
  66. <li>香蕉<a href="">×</a></li>
  67. <li>榴莲<a href="">×</a></li>
  68. <li>火龙果<a href="">×</a></li>
  69. </ul>
  70. <div>
  71. <input type="text" id="fname">
  72. <button id="ok">确定</button>
  73. </div>
  74. </div>
  75. <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
  76. <script>
  77. // 1. $函数的参数是一个函数,该函数是页面加载完成后执行的回调函数
  78. $(() => {
  79. function removeItem(evt) {
  80. evt.preventDefault()
  81. // 4. $函数的参数是原生JavaScript对象,返回该原生JavaScript对象对应的jQuery对象
  82. $(evt.target).parent().remove()
  83. }
  84. function addItem(evt) {
  85. let fname = $('#fname').val().trim()
  86. if (fname.length > 0) {
  87. $('#fruits').append(
  88. // 3. $函数的参数是标签字符串,创建对应的标签元素并返回jQuery对象
  89. $('<li>').text(fname).append(
  90. $('<a>').attr('href', '').text('×')
  91. .on('click', removeItem)
  92. )
  93. )
  94. }
  95. $('#fname').val('')
  96. // jQuery对象通过下标运算或get方法可以获得与之对应的原生JavaScript对象
  97. // input.get(0).focus()
  98. $('#fname')[0].focus()
  99. }
  100. // 2. $函数的参数是选择器字符串,返回对应元素的jQuery对象
  101. $('#fruits a').on('click', removeItem)
  102. $('#ok').on('click', addItem)
  103. $('#fname').on('keydown', (evt) => {
  104. let code = evt.keyCode || evt.which
  105. if (code == 13) {
  106. addItem(evt)
  107. }
  108. })
  109. })
  110. </script>
  111. </body>
  112. </html>

项目描述测试

Contributors (2)