Detailed explanation of JQuery element operations
This time I will bring you a detailed explanation of the JQuery element operation. What are the precautions for JQuery element operation? The following is a practical case, let's take a look.
Step one: sizzle selector
"Find" (or select) based on the element's id, class, type, attribute, attribute value, etc.HTML Element is simply based on css selector , in addition to some specific selectors.
Step 2: Query ancestors
parent()Returns the direct parent element of the selected element. This method will only go up one level. DOM tree traversalparents()Optional parameters can be used to filter the search for parent elementsReturns all ancestor elements of the selected element, all the way up to the root of the document Element
$(document).ready(function(){ //会返回span开始到p为止的祖先元素 $("span").parentsUntil("p"); });
Step 3: Query Descendants
children()You can use optional parameters to filter the search for child elementsReturns all direct child elements of the selected element. This method only The DOM tree will be traversed to the next levelfind()You can use optional parameters to filter the search for elementsReturns the descendant elements of the selected element. All the way down to the last descendantStep 4: Query siblings
siblings()Return all sibling elements of the selected elementnext()Returns the next sibling element of the selected elementnextAll()Returns all sibling elements after the selected elementnextUntil()Returns all following sibling elements between the two given arguments
$(document).ready(function(){ //返回介于 <h2>与<h6>元素之间的所有同胞元素 $("h2").nextUntil("h6"); });
Step 5: Add filtering when querying
first()Return the first element among the selected elements last()Returns the last element among the selected elementseq()Returns the element with the specified index number among the selected elements. This is easy to understand. , an example is: $(element[flag]) has the same result as element.eq(flag)filter() Filters the query results, similar to not() below, but has the opposite effect not()Return all elements that do not match the standard$(document).ready(function(){ //返回不带有类名"target"的所有p元素 $("p").not(".target"); });
Step 6: text(), html(), val() and attr()
text(), html(), val() and attr (), hascallback function. The callback function takes two parameters: the index of the current element in the selected element list, and the original (old) value. Then return the string you wish to use as the function's new value
1.text() - Sets or returns the text content of the selected element 2.html() - Set or return the content of the selected element (including HTML tags) 3.val() - Set or return the value of the form field 4.attr() - Set or return the attribute value$("#btn1").click(function(){ $("#test1").text(function(i,origText){ return "旧文本: " + origText + " 新文本: index: " + i; }); });
Step 7: Add elements
append() - Insert content at the end of the selected element prepend() - After the selected element Insert content at the beginning of the selected element after() - Insert content after the selected element before() - Insert content before the selected elementStep 8: Delete element
remove()可接受一个参数,允许你对被删元素进行过滤,empty()不可以
remove() - 删除被选元素(及其子元素)
empty() - 从被选元素中删除子元素
//等同于$("p.target").remove(); $("p").remove(".target");
第九步:替换元素
replaceAll()和replaceWith()功能类似,但是目标和源相反
replaceWith() - 用提供的内容替换集合中所有匹配的元素并且返回被删除元素的集合
replaceAll() - 用集合的匹配元素替换每个目标元素
第十步:class操作
addClass() - 向被选元素添加一个或多个类
removeClass() - 从被选元素删除一个或多个类
toggleClass() - 对被选元素进行添加/删除类的切换操作
hasClass() - 判断一个元素是否存在该class
第十一步:css()方法
设置或返回被选元素的一个或多个样式属性
css("propertyname"); - 返回propertyname属性的值
css("propertyname","value"); - 设置propertyname属性的值
css({"propertyname":"value","propertyname":"value",...}); - 设置多个值
第十二步:元素尺寸
width() 方法设置或返回元素的宽度(不包括内边距、边框或外边距)
height() 方法设置或返回元素的高度(不包括内边距、边框或外边距)
innerWidth() 方法返回元素的宽度(包括内边距)
innerHeight() 方法返回元素的高度(包括内边距)
outerWidth() 方法返回元素的宽度(包括内边距和边框)
outerHeight() 方法返回元素的高度(包括内边距和边框)
相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
推荐阅读:
The above is the detailed content of Detailed explanation of JQuery element operations. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Windows operating system is one of the most popular operating systems in the world, and its new version Win11 has attracted much attention. In the Win11 system, obtaining administrator rights is an important operation. Administrator rights allow users to perform more operations and settings on the system. This article will introduce in detail how to obtain administrator permissions in Win11 system and how to effectively manage permissions. In the Win11 system, administrator rights are divided into two types: local administrator and domain administrator. A local administrator has full administrative rights to the local computer

Detailed explanation of division operation in OracleSQL In OracleSQL, division operation is a common and important mathematical operation, used to calculate the result of dividing two numbers. Division is often used in database queries, so understanding the division operation and its usage in OracleSQL is one of the essential skills for database developers. This article will discuss the relevant knowledge of division operations in OracleSQL in detail and provide specific code examples for readers' reference. 1. Division operation in OracleSQL

How to use PUT request method in jQuery? In jQuery, the method of sending a PUT request is similar to sending other types of requests, but you need to pay attention to some details and parameter settings. PUT requests are typically used to update resources, such as updating data in a database or updating files on the server. The following is a specific code example using the PUT request method in jQuery. First, make sure you include the jQuery library file, then you can send a PUT request via: $.ajax({u

The modulo operator (%) in PHP is used to obtain the remainder of the division of two numbers. In this article, we will discuss the role and usage of the modulo operator in detail, and provide specific code examples to help readers better understand. 1. The role of the modulo operator In mathematics, when we divide an integer by another integer, we get a quotient and a remainder. For example, when we divide 10 by 3, the quotient is 3 and the remainder is 1. The modulo operator is used to obtain this remainder. 2. Usage of the modulo operator In PHP, use the % symbol to represent the modulus

Title: jQuery Tips: Quickly modify the text of all a tags on the page In web development, we often need to modify and operate elements on the page. When using jQuery, sometimes you need to modify the text content of all a tags in the page at once, which can save time and energy. The following will introduce how to use jQuery to quickly modify the text of all a tags on the page, and give specific code examples. First, we need to introduce the jQuery library file and ensure that the following code is introduced into the page: <

Title: Use jQuery to modify the text content of all a tags. jQuery is a popular JavaScript library that is widely used to handle DOM operations. In web development, we often encounter the need to modify the text content of the link tag (a tag) on the page. This article will explain how to use jQuery to achieve this goal, and provide specific code examples. First, we need to introduce the jQuery library into the page. Add the following code in the HTML file:

How to tell if a jQuery element has a specific attribute? When using jQuery to operate DOM elements, you often encounter situations where you need to determine whether an element has a specific attribute. In this case, we can easily implement this function with the help of the methods provided by jQuery. The following will introduce two commonly used methods to determine whether a jQuery element has specific attributes, and attach specific code examples. Method 1: Use the attr() method and typeof operator // to determine whether the element has a specific attribute

jQuery is a popular JavaScript library widely used in web development. During web development, it is often necessary to dynamically add new rows to tables through JavaScript. This article will introduce how to use jQuery to add new rows to a table, and provide specific code examples. First, we need to introduce the jQuery library into the HTML page. The jQuery library can be introduced in the tag through the following code:
