This time I will show you how to quickly operate JQuery elements, and what are the precautions for operating JQuery elements. The following is a practical case, let's take a look.
First, let’s take a look at how to find the desired node in jquery.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"); });</h6> </h2>prev(), prevAll() and prevUntil() The prev(), prevAll() and prevUntil() methods work similarly to the above methods, except in the opposite direction: they return the previous sibling elements (traversing along the previous sibling elements in the DOM tree, and Not after element traversal).
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"); });After the element is found, we then need to operate on the found node according to the requirements.
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 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 How to quickly operate JQuery elements. For more information, please follow other related articles on the PHP Chinese website!

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.

JavaScript originated in 1995 and was created by Brandon Ike, and realized the language into C. 1.C language provides high performance and system-level programming capabilities for JavaScript. 2. JavaScript's memory management and performance optimization rely on C language. 3. The cross-platform feature of C language helps JavaScript run efficiently on different operating systems.

JavaScript runs in browsers and Node.js environments and relies on the JavaScript engine to parse and execute code. 1) Generate abstract syntax tree (AST) in the parsing stage; 2) convert AST into bytecode or machine code in the compilation stage; 3) execute the compiled code in the execution stage.

The future trends of Python and JavaScript include: 1. Python will consolidate its position in the fields of scientific computing and AI, 2. JavaScript will promote the development of web technology, 3. Cross-platform development will become a hot topic, and 4. Performance optimization will be the focus. Both will continue to expand application scenarios in their respective fields and make more breakthroughs in performance.

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

Yes, the engine core of JavaScript is written in C. 1) The C language provides efficient performance and underlying control, which is suitable for the development of JavaScript engine. 2) Taking the V8 engine as an example, its core is written in C, combining the efficiency and object-oriented characteristics of C. 3) The working principle of the JavaScript engine includes parsing, compiling and execution, and the C language plays a key role in these processes.

JavaScript is at the heart of modern websites because it enhances the interactivity and dynamicity of web pages. 1) It allows to change content without refreshing the page, 2) manipulate web pages through DOMAPI, 3) support complex interactive effects such as animation and drag-and-drop, 4) optimize performance and best practices to improve user experience.

C and JavaScript achieve interoperability through WebAssembly. 1) C code is compiled into WebAssembly module and introduced into JavaScript environment to enhance computing power. 2) In game development, C handles physics engines and graphics rendering, and JavaScript is responsible for game logic and user interface.


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

Atom editor mac version download
The most popular open source editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 English version
Recommended: Win version, supports code prompts!

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