Home >Web Front-end >JS Tutorial >What is $() in jquery library
is the jquery object, $() is jQuery(), you can pass parameters in it, and its function is to get the element
##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!