Home >Web Front-end >JS Tutorial >Usage examples of add() method in jQuery_jquery

Usage examples of add() method in jQuery_jquery

WBOY
WBOYOriginal
2016-05-16 16:21:311259browse

The example in this article describes the usage of the add() method in jQuery. Share it with everyone for your reference. The specific analysis is as follows:

This function adds elements matching an expression to the matching elements.
The results returned by the add() function will always be sorted in the order in which the elements appear in the HTML document, rather than simply added.

Grammar:

Grammar 1:

Copy code The code is as follows:
$(selector).add(expr,context)

Parameter
参数 描述
expr 可选。用于匹配元素的选择器字符串。
context 可选。作为待查找的 DOM 元素集、文档或 jQuery 对象。
Description
expr Optional. The selector string used to match elements.
context Optional. As a set of DOM elements, documents, or jQuery objects to be found.

Example:

Example 1:

Add span elements to matching elements and set their font color to green.

Copy code The code is as follows:






Script Home








  • I am p



  • I am span

I am span



Example 2:

Copy code The code is as follows:






Script Home








  • I am p



  • I am span

I am span



Grammar 2:

Copy code The code is as follows:
$(selector).add(element)

参数列表:
参数
参数 描述
element 可选。被添加的DOM元素或者jQuery元素
描述
element 可选。被添加的DOM元素或者jQuery元素

实例:

实例一:

复制代码 代码如下:






脚本之家






 

       

  •      

    我是p


       

  •     我是span
     

  我是span



实例二:

复制代码 代码如下:






脚本之家






 

       

  •      

    我是p


       

  •     我是span
     

  我是span



语法三:

复制代码 代码如下:
$(selector).add(html)

参数列表:
参数
参数 描述
html 可选。被添加的html代码片段
描述
html 可选。被添加的html代码片段

实例:

代码如下:







脚本之家





 

       

  •      

    我是p


       

  •  


希望本文所述对大家的jQuery程序设计有所帮助。
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