Blogger Information
Blog 37
fans 2
comment 0
visits 26674
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
仿jquery选择器 ,事件,css封装
世纪天城
Original
567 people have browsed it
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>js封装3</title>
  8. </head>
  9. <body>
  10. <div id="div">
  11. <button id="hide" class="hide">hide</button>
  12. <button id="show" class="show">show</button>
  13. <div> <img id="img" src="../a/images/1.png" alt=""></div>
  14. <input type="text" id="input">
  15. <p id="onmouseover">鼠标移入事件</p>
  16. <ul>
  17. <li>1</li>
  18. <li>2</li>
  19. <li>3</li>
  20. </ul>
  21. </div>
  22. <script>
  23. let f = function(selector){
  24. this.querySelectorAll(selector);
  25. }
  26. f.prototype.querySelectorAll = function(selector){
  27. this.element = document.querySelectorAll(selector);
  28. return this.element;
  29. }
  30. // // 隐藏
  31. f.prototype.hide =function(){
  32. for(i=0;i<this.element.length;i++){
  33. this.element[i].style.display = 'none';
  34. }
  35. }
  36. // 显示
  37. f.prototype.show =function(){
  38. for(i=0;i<this.element.length;i++){
  39. this.element[i].style.display = 'block';
  40. }
  41. }
  42. // 事件
  43. f.prototype.click = function(click,ev){
  44. let c = click;
  45. // console.log(c);
  46. switch(true){
  47. //点击事件
  48. case c === 'onclick':
  49. this.element[0].onclick = ev;
  50. break;
  51. // 失去焦点事件
  52. case c === "onblur":
  53. this.element[0].onblur = ev;
  54. break;
  55. // 元素获得焦点
  56. case c === "onfocus":
  57. this.element[0].onfocus = ev;
  58. break;
  59. // 鼠标移入事件
  60. case c === "onmouseover":
  61. this.element[0].onmouseover = ev;
  62. break;
  63. // 鼠标移除事件
  64. case c === "onmouseout":
  65. this.element[0].onmouseout = ev;
  66. break;
  67. }
  68. }
  69. // css样式
  70. f.prototype.css = function(attr,value){
  71. this.element[0].style[attr] = value;
  72. }
  73. $ = function (selector){
  74. return new f(selector);
  75. }
  76. // 实例调试
  77. // 获取元素
  78. let hide = $('.hide');
  79. $('#hide');
  80. console.log($('#hide'));
  81. // 隐藏
  82. $('#hide').click('onclick',function(){
  83. $('#img').hide();
  84. });
  85. // 显示
  86. $('.show').click('onclick',function(){
  87. $('#img').show();
  88. });
  89. // 失去焦点
  90. $('#input').click('onblur',function(){
  91. console.log(1111);
  92. });
  93. // 元素获得焦点
  94. $('#input').click('onfocus',function(){
  95. console.log(2222);
  96. });
  97. // 鼠标移入事件
  98. $('#onmouseover').click('onmouseover',function(){
  99. console.log(3333);
  100. });
  101. // 鼠标移除事件
  102. $('#onmouseover').click('onmouseout',function(){
  103. console.log(4444);
  104. });
  105. // css样式
  106. $('ul').css('color','red');
  107. $('ul').css('background','coral');
  108. $('ul').css('width','100px');
  109. </script>
  110. </body>
  111. </html>
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post