博主信息
博文 37
粉丝 2
评论 0
访问量 34585
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
仿jquery选择器 ,事件,css封装
世纪天城
原创
792人浏览过
  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>
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学