您好,欢迎访问一九零五行业门户网

jQuery的用法介绍(代码)

本篇文章给大家带来的内容是关于jquery的用法介绍(代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
代码部分
<script>        window.jquery=function(nodeorselector){            let nodes={}            let node1=[]            if(typeof nodeorselector==='string'){               let temp=document.queryselectorall(nodeorselector)               for(let i=0;i<temp.length;i++){ node1[i]=temp[i] } }else if(nodeorselector instanceof node){ node1={ 0:nodeorselector, length:1 } } nodes.addclass=function(classes){ classes.foreach(value => {                    for(let i=0;i<node1.length;i++){ node1[i].classlist.add(value) } }) } nodes.settext=function(text){ for(let i=0;i<node1.length;i++){ node1[i].textcontent=text } } return nodes } window.$=jquery var $p=$('p') $p.addclass(['red']) $p.settext('hi')</script>
思路首先声明函数jquery()并在函数中声明一个封装了两个函数的对象,以实现给元素加class和添加文本的功能;
其次在函数中判断所传参数是否为字符串还是节点;
接着编写addclass与settext方法;
最后返回对象nodes,可以调用对象中的方法,大功告成。
本篇文章到这里就已经全部结束了,更多其他精彩内容可以关注的jquery视频教程栏目!
以上就是jquery的用法介绍(代码)的详细内容。
其它类似信息

推荐信息