Home Web Front-end JS Tutorial jQuery study notes change jQuery object_jquery

jQuery study notes change jQuery object_jquery

May 16, 2016 pm 05:49 PM
Change

jQuery代码的任务就是生成jQuery对象A,操作jQuery对象A;生成jQuery对象B,操作jQuery对象B……但是若此过程中,对象A、B……之间有某种关系,那么完全没必要一个个去$(selector),这很繁琐的。因此jQuery提供了一些方法,使流程变为生成jQuery对象A,操作jQuery对象A;更改为jQuery对象B,操作jQuery对象B……

  一个jQuery对象,既要进行N次操作,又要进行M次更改。因此有必要将生成的jQuery对象存储在一个变量中,多次调用。然而,试想每进行一次操作和更改就得声明一个新变量,这也很繁琐啊。所以jQuery采取了链式操作的方法,即执行操作后返回操作对象本身,于是可以持续执行下一个操作,直到需要更改对象时方执行更改,然后返回更改后对象。这实际上就是一种函数式思维。

  举个例子,左右对比一下:

一般调用

链式调用

a=$(“div”);

a.addClass(“class”);

b=a.children(“ul”);

b.show();                         

c=a.siblings();

c.removeClass(“class”);

$(“div”).addClass(“class”)

.children(“ul”).show().end()

.siblings().removeClass(“class”);

  接下来就介绍一下更改jQuery对象的各种方法:

更改为后代元素集合

方法

描述

等价

children(selector)

在原先元素的后代元素中,选取匹配selector的元素。若不设置参数,children()等价于children(*),选取原先元素的所有子元素

$(selector1).children(selector2)≡$(selector1>selector2)

find(selector)

在原先元素的后代元素中,选取匹配selector的元素。若不设置参数,find()等价于find(“:not(*)”),不会选取原先元素的任何后代元素

$(selector1).find(selector2)≈$(selector1 selector2)。若参数使用基本过滤选择器,不是在全部后代元素中选取过滤匹配元素,而是在每一个后代元素中分别选取过滤匹配元素

contents()

选取原先元素的子元素或文本块

 

更改为祖先元素集合

方法

描述

parent(selector)

在原先元素的父元素中,选取匹配selector的元素。若不设置参数,parent()等价于parent(“*”),选取原先元素的所有父元素

parents(selector)

在原先元素的祖先元素中,选取匹配selector的元素。若不设置参数,parents()等价于parents(“*”),选取原先元素的所有祖先元素

parentsUntil(selector)

选取原先元素的祖先元素,直到遇到匹配selector的元素为止,且不包括该元素。若不设置参数,parentsUntil()等价于parents(),选取原先元素的所有祖先元素

offsetParents()

选取原先元素的最近祖先定位元素,且该元素CSS属性display不能为none。定位元素指CSS属性position

closest(selector)

在原先元素及其祖先元素中,选取匹配selector的最近元素

 

更改为兄弟元素集合

方法             

描述

等价

next(selector)

在原先元素后面的第一个兄弟元素中,选取匹配selector的元素。若不设置参数,next()等价于next(“*”),选取原先元素后面的第一个兄弟元素

$(selector1).next(selector2)≡$(selector1+selector2)

prev(selector)

在原先元素前面的第一个兄弟元素中,选取匹配selector的元素。若不设置参数,prev()等价于prev(“*”),选取原先元素前面的第一个兄弟元素

nextAll(selector)

在原先元素后面的兄弟元素中,选取匹配selector的元素。若不设置参数,nextAll()等价于nextAll(“*”),选取原先元素后面的所有兄弟元素

$(selector1).nextAll(selector2)≡$(selector1~selector2)

prevAll(selector)

在原先元素前面的兄弟元素中,选取匹配selector的元素。若不设置参数,prevAll()等价于prevAll(“*”),选取原先元素前面的所有兄弟元素

siblings(selector)

在原先元素的兄弟元素中,选取匹配selector的元素。若不设置参数,siblings()等价于siblings(“*”),选取原先元素的所有兄弟元素

nextUntil(selector)

选取原先元素后面的兄弟元素,直到遇到匹配selector的元素为止,且不包括该元素。若不设置参数,nextUntil()等价于nextAll(),选取原先元素后面的所有兄弟元素

prevUntil(selector)

选取原先元素前面的兄弟元素,直到遇到匹配selector的元素为止,且不包括该元素。若不设置参数,prevUntil()等价于prevAll(),选取原先元素前面的所有兄弟元素

 

更改为更多元素集合

方法             

描述

等价

add(selector)

在原先元素的基础上添加选取匹配selector的元素

$(selector1).add(selector2)≡$(selector1,selector2)

andSelf()

更改为后代元素、祖先元素、兄弟元素的这些操作,会在原先元素以外选取元素。可用于将原先元素和更改操作选取的元素合并在一起

is changed to a collection of partial elements

方法             

描述

等价

eq(index)

在原先元素中筛选索引值等于index的元素,索引值从0开始正数,也可以从-1开始倒数,但不能混用

$(selector).eq(index)≡$(selector:eq(index))

first()

在原先元素中筛选第一个元素,等同于eq(0)

$(selector).first()≡$(selector:first)

last()

在原先元素中筛选最后一个元素,等同于eq(-1)

$(selector).last()≡$(selector:last)

slice(start,[end])

在原先元素中筛选索引值从start到end-1的元素。若不传入end,则筛选索引值大于等于start的元素

filter(selector)

在原先元素中筛选匹配selector的元素

filter(fn(index))

使用函数筛选,对于索引值等于index的元素,若函数返回true,该元素包含在筛选集合中,否则排除在外

可实现$(selector:even(index))、$(selector:odd(index))、

$(selector:gt(index))、

$(selector:lt(index))等

not(selector)

在原先元素中筛选不匹配selector的元素

$(selector1).not(selector2)≡$(selector1:not(selector2))

not(fn(index))

使用函数筛选,对于索引值等于index的元素,若函数返回true,该元素排除在筛选集合外,否则包含在内

可实现$(selector:even(index))、$(selector:odd(index))、

$(selector:gt(index))、

$(selector:lt(index))等

has(selector)

在原先元素中筛选出拥有匹配selector后代元素的元素

$(selector1).has(selector2)≡$(selector1:has(selector2))

Method                                                       

Description

Equivalent

方法             

描述

end()

使执行更改jQuery对象操作后的选取元素还原到更改之前。若希望还原多个更改操作,则多次调用,直到最后会返回空集

eq(index) Select the elements whose index value is equal to index among the original elements. The index value starts from 0 as a positive number, or it can count down from -1, but it cannot be mixed $(selector).eq(index)≡$(selector:eq(index))
first() Select the first element among the original elements, which is equivalent to eq(0) $(selector).first()≡$(selector:first)
last() Select the last element among the original elements, which is equivalent to eq(-1) $(selector).last()≡$(selector:last)
slice(start,[end]) Select elements with index values ​​from start to end-1 in the original elements. If end is not passed in, elements with index values ​​greater than or equal to start will be filtered
filter(selector) Filter elements matching the selector from the original elements
filter(fn(index)) Use function filtering. For elements whose index value is equal to index, if the function returns true, the element is included in the filtered collection, otherwise it is excluded Can realize $(selector:even(index)), $(selector:odd(index)), $(selector:gt(index))、 $(selector:lt(index)) etc.
not(selector) Filter elements that do not match the selector from the original elements $(selector1).not(selector2)≡$(selector1:not(selector2))
not(fn(index)) Use function filtering. For elements whose index value is equal to index, if the function returns true, the element will be excluded from the filtered collection, otherwise it will be included Can realize $(selector:even(index)), $(selector:odd(index)), $(selector:gt(index))、 $(selector:lt(index)) etc.
has(selector) Filter out elements that have descendant elements that match the selector from the original elements $(selector1).has(selector2)≡$(selector1:has(selector2))
Restore jQueryObject
Method                                                        Description
end() Restore the selected elements after changing the jQuery object to before the change. If you want to restore multiple change operations, call it multiple times until an empty set is returned in the end
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

4 steps to change user folder names on Windows 11 4 steps to change user folder names on Windows 11 Jul 07, 2023 pm 02:33 PM

User folder names and account names are set during user account setup. However, sometimes for some reason, you need to change your user folder name on Windows 11. Unlike renaming a standard folder, this process is not that simple. But with the right information, the process can be effortless, and that's what we've got you covered in this guide. Is it safe to rename my user folder name? As mentioned before, changing user folder names on Windows 11 is not as simple as renaming a normal folder. Even if you change the user account name, the user folder name will remain the same. Microsoft recommends not changing your user folder name as this may cause some applications to

How to change the storage location of wallpaper engine? How to set the save path in wallpaper engine How to change the storage location of wallpaper engine? How to set the save path in wallpaper engine Mar 13, 2024 pm 12:40 PM

Many users like to download various wallpapers and videos on WallpaperEngine. Over time, they will find that more and more wallpapers are downloaded, resulting in insufficient hard disk space. At this time, the storage location of WallpaperEngine can be changed to reduce the space occupied. So let’s take a look at how to change the save path for wallpaperengine. Step 1: Click Settings under steam in the upper left corner to open the following interface. Step 2: Click Download to find the "Steam Library Folder" under the content library, and click Open above. Step 3: Click Add Library Folder, select the path you want to change to, and after adding it, right-click on the default column.

How to change the font color of win7 desktop icons How to change the font color of win7 desktop icons Jan 02, 2024 pm 11:17 PM

The default desktop icon font of win7 is generally white. If we use a white desktop background, the desktop icon text may not be visible. At this time, we can customize the desktop font color through the advanced appearance settings in the personalization settings. The following is Let’s take a look together. Tutorial on changing the font color of win7 desktop icons 1. Right-click a blank space on the desktop and open the "Personalization" settings. 2. Under Theme, we can directly select the desired theme to change the font color of desktop icons. 3. If you are not satisfied with these themes, you can also turn on the "Window Color" as shown in the picture. 4. Click "Advanced Appearance Settings" below 5. Change the "Project" at the icon location to "Desktop" 6. Then you can change various attributes such as font color and size in the red box

How to adjust the font, style, and size of Notepad in Windows 11 How to adjust the font, style, and size of Notepad in Windows 11 Sep 23, 2023 pm 11:25 PM

Many users want to change the font in Notepad on Windows 11 because the default font is too small or difficult to read for them. Changing fonts is quick and easy, and in this guide, we'll show you how to customize Notepad and change the font to suit your needs. What font does Windows 11 Notepad use by default? As for the default font options, Notepad uses the Consolas font and the default font size is set to 11 pixels. How to change Notepad font size and style in Windows 11? Use the Edit menu in Notepad to click the search button and type notepad. Select Notepad from the list of results. In Notepad, click the Edit menu and select Fonts. You should now see the settings in the left pane

Step-by-step guide to changing background color with Eclipse Step-by-step guide to changing background color with Eclipse Jan 28, 2024 am 08:28 AM

Teach you step by step how to change the background color in Eclipse, specific code examples are required Eclipse is a very popular integrated development environment (IDE) that is often used to write and debug Java projects. By default, the background color of Eclipse is white, but some users may wish to change the background color to suit their preference or to reduce eye strain. This article will teach you step by step how to change the background color in Eclipse and provide specific code examples. Step 1: Open Eclipse First

How to change region settings on xbox store How to change region settings on xbox store Dec 24, 2023 pm 08:53 PM

When the game you want to buy is not available, you can purchase it by changing the region. Do any players know how to change the region settings in the Xbox store? So let’s take a look at the introduction to changing the region settings in the Xbox store! Xbox store region settings: 1. Open windows settings - select time and language. 2. Select the region - the default should be China - select other countries and regions. 3. Select other countries and regions - enter the store - the store prompts you to refresh the content.

Teach you how to modify the temporary file location of Win7 Teach you how to modify the temporary file location of Win7 Jan 04, 2024 pm 11:25 PM

The temp folder is our temporary file storage location. The system will save temporary files to this folder. If there are too many temporary files, especially when the temp folder is on the system disk, it is likely to affect the system running speed. We can solve the problem by changing the temp location. Let’s take a look below. Tutorial on changing the location of win7temp 1. First, right-click "Computer" and open "Properties" 2. Click "Advanced System Settings" on the left 3. Click "Environment Variables" below 4. Select "temp" and click "Edit" 5. Then change Just change the "Variable Value" to the path that needs to be changed.

Teach you how to modify the startup sequence of win7 Teach you how to modify the startup sequence of win7 Jan 04, 2024 pm 09:06 PM

Properly setting the startup sequence of win7 can make our computer boot faster, but many friends don’t know how to change the startup sequence. In fact, we can change it in the registry editor. Let’s take a look at it with the editor. A specific method. Tutorial on changing the win7 startup sequence 1. Search for "Run" in the lower left corner. 2. Open the "Run" program. 3. Enter “regedit” and press Enter to confirm. 4. Find the following path "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\ServiceGroupOrder" and right-click to modify the icon "list" file. 5. The position shown in the picture is from top to bottom.

See all articles