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

What is $() in jquery library

(*-*)浩
(*-*)浩Original
2019-06-01 15:43:158495browse

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>

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对象'

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对象'

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!

Statement:
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
Previous article:Is jquery easyui charged?Next article:Is jquery easyui charged?