Home > Web Front-end > JS Tutorial > What is $() in jquery library

What is $() in jquery library

(*-*)浩
Release: 2019-06-15 17:58:53
Original
8505 people have browsed it

is the jquery object, $() is jQuery(), you can pass parameters in it, and its function is to get the element

What is $() in jquery library

##The following example

$(".div1") means to get the element with the class name div1, for example, get

$(".div1").onclick represents the click event of the div with the class name div1

$ is the jQuery object, which is a Function object. $() calls this function and gets


Example:

html

<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ul>
Copy after login

js

Method One

 // 传入DOM元素
        $('li').each(function(index,ele){
               $(ele).on('click',function(){
                   $(this).css('background','red');//这里的DOM元素就是this
               })
       })
       
       //传入DOM数组
       var aLi=document.getElementsByTagName('li');
           aLi=[].slice.call(aLi);//集合转数组
           var $aLi=$(aLi);
           $aLi.html('我是jQuery对象');//所有的li的内容都变成'我是jQuery对象'
Copy after login

Method Two

 // 传入DOM元素
        jQuery('li').each(function(index,ele){
               jQuery(ele).on('click',function(){
                   jQuery(this).css('background','red');//这里的DOM元素就是this
               })
       })
       
       //传入DOM数组
       var aLi=document.getElementsByTagName('li');
           aLi=[].slice.call(aLi);//集合转数组
           var $aLi=$(aLi);
           $aLi.html('我是jQuery对象');//所有的li的内容都变成'我是jQuery对象'
Copy after login

The above is the detailed content of What is $() in jquery library. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
js
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template