Home > Web Front-end > JS Tutorial > body text

7 ways to use jQuery $() function (summary)

青灯夜游
Release: 2020-12-30 09:11:36
forward
2616 people have browsed it

7 ways to use jQuery $() function (summary)

Recommended tutorial: jQuery tutorial

jQuery object is an array-like object, containing consecutive Integrated properties and a series of jQuery methods. It wraps all operations in a jQuery() function, forming a unified (and only) operation entrance. One of the functions we use very frequently is $() or jQuery(). When we call it, different effects will be achieved depending on the parameters passed in.

Next, I will talk about these 7 usages one by one. You are welcome to correct any inaccuracies among them.

1 jQuery(selector,context)

Briefly: receive a css selector expression (selector) and optional selector context (context), returns a jQuery object containing the matching DOM element.

By default, the search for matching elements starts from the

root element ducument object, which means that the search scope is the entire document tree. But if context context is given, search

html

    <span>body span</span>
    <span>body span</span>
    <span>body span</span>
    <div class="wrap">
        <span>wrap span</span>
        <span>wrap span</span>
        <span>wrap span</span>
    </div>
Copy after login

js

$(&#39;span&#39;).css(&#39;background-color&#39;,&#39;red&#39;);//所有的span都会变红
$(&#39;span&#39;,&#39;.wrap&#39;).css(&#39;background-color&#39;,&#39;red&#39;);//只有.wrap中的span会变红
Copy after login

## in the specified context. #2 jQuery(html,ownerDocument),jQuery(html,props)

Create DOM elements using the provided html code

对于jQuery(html,ownerDocument),参数html可以是单标签或者是多层标签之间的嵌套。第二个参数用于创建新DOM元素的文档对象,如果不传入则默认为当前的文档对象。

    //单标签  两种方式都可以往body中插入div
    /*   
     *   1  $(&#39;<div>&#39;).appendTo(&#39;body&#39;);
     *   2  $(&#39;<div></div>&#39;).appendTo(&#39;body&#39;);  
     */
    // 多标签嵌套
     $(&#39;<div><span>dfsg</span></div>&#39;).appendTo(&#39;body&#39;);
Copy after login

另外:对于单标签,jQuery(html,props),props是一个包含属性和事件的普通的对象,用法如下。(该用法有待考证,请知道这一用法的童鞋告知一下,感激不尽)

        $(&#39;<div>我是div</div>&#39;,{
            title:&#39;我是新的div&#39;,
            click:function(){
                $(this).css(&#39;color&#39;,&#39;red&#39;);
                console.log(this);
            }
        }).appendTo(&#39;body&#39;);
Copy after login

3 jQuery(element or elementsArray)

如果传入一个DOM元素或者是DOM元素的数组,则把DOM元素封装到jQuery对象中并返回。

html

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

js

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

4 jQuery(object)

如果传入的是一个object对象,则把该对象封装到jQuery对象中并返回。

    var obj={name:&#39;谦龙&#39;};
    var $obj=$(obj);//封装成jQuery对象
    //绑定自定义事件
    $obj.on(&#39;say&#39;,function(){
        console.log(this.name)//输出谦龙
    });
    $obj.trigger(&#39;say&#39;);
Copy after login

5 jQuery(callback)

当传进去的参数是函数的时候,则在document对象上绑定一个ready事件监听函数,当DOM结构加载完成的时候执行

    $(function(){
    
    })
    //以上代码和下面的效果是一样的
    $(document).ready(function(){
        ...//代码
    })
Copy after login

6 jQuery(jQuery object)

当传进去的参数是一个jQuery对象的时候,则创建该jQuery对象的一个副本并返回。副本与传入的jQuery对象引用完全相同的元素

    var aLi=$(&#39;li&#39;);
    var copyLi=$(aLi);//创建一个aLi的副本
    console.log(aLi);
    console.log(copyLi);
    console.log(copyLi===aLi);
Copy after login

7 ways to use jQuery $() function (summary)

7 jQuery()

如果不传入任何的参数,则返回一个空的jQuery对象,属性length为0

注意这个功能可以用来复用jQuery对象,例如可以创建一个空的jQuery对象,然后在需要的时候先手动修改其中的元素,然后在调用jQuery方法。从而避免重复创建jQuery对象。

更多编程相关知识,请访问:编程教学!!

The above is the detailed content of 7 ways to use jQuery $() function (summary). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
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