Home Web Front-end JS Tutorial jQuery Lesson 3 Code to Modify Element Attributes and Content_jquery

jQuery Lesson 3 Code to Modify Element Attributes and Content_jquery

May 16, 2016 pm 06:32 PM
jquery element attributes content

1. Operation attributes
The above describes how to filter out the required elements. After getting the element, you need to operate on it. A common requirement is to iterate through the obtained set of elements and perform an operation on each element. The function provided by jQuery is
each(iterator), where iterator is a function that accepts an integer as a parameter, indicating the number of elements. Let's look at a simple example.

Copy code The code is as follows:



jQuery Operation




Image Gallery
March 13th,2010










Check the results using Firebug:

image

In the above example, the native JavaScript method is used to access attributes. jQuery provides a more general method to access attributes, attr:

attr(name), if name is a string, get the value of name of the attribute of the first element; if name is an object, the attributes of the object are copied to all elements of the packaging set as attributes of the element .

attr(name,value), when name is a string, set the value of attribute name to value. When value is a function, call this function for each element in the packaging set to set it The value of name is set to the return value of the function.

Look at a simple example, the html code still uses the above:

Copy the code The code is as follows:


$(function() {
$('body').children().attr('title', function(n) {
return"This is " n "th element";
});
$('img').attr('alt', 'A photo taken by YinZixin');
alert($ ('h1').attr('title'));
});


The result is:
image
To delete an attribute, use the removeAttr(name) method.
Please note that there is a special attribute class among the attributes. The class attribute is very commonly used, and it happens to be a keyword in JavaScript. To access the class attribute, use className instead of class. For example:
$('img').attr('className', 'photo'); class is different from other attributes. An element can have multiple classes, separated by spaces, for example class='big strong ', because class is special and commonly used, jQuery has a special method to handle class attributes.
addClass(names), add a class;
removeClass(names), delete a class;
toggleClass(names), if the element has the class, delete it, otherwise add it.
The names is a string, which can be composed of multiple class names separated by spaces. addClass and removeClass are easy to use without giving examples. toggleClass is particularly simple and practical. Let’s look at an example below. The html code still uses the above:
Copy code The code is as follows:




$(function() {
$('td:odd').append($('span'));
$('div:first').append($( 'p')).append('sub title');
});








Hello JQuery

Title





The final result is as follows:
image
There is also appendTo(target), which is the opposite direction of the append method. append adds parameters to the caller, and appendTo The caller adds the parameters internally. There are several pairs of methods similar to append and appendTo:

prepend, prependTo:append method. When there are other elements inside the target element, the added element is at the end of the original element, and prepend is at the front. .

before, insertBefore: Insert before the destination element, not inside.

after, insertAfter: Insert after the destination element.

To delete elements, you can use the remove or empty method. Note that remove will delete the selected elements from the page and return these elements as return values. These elements will not be garbage collected. They can be further operated on or redisplayed on the page using methods such as append. The empty method completely deletes the element.

3. Manipulating the values ​​of form elements
Manipulating the values ​​of form elements is very common, but it is not easy. jQuery provides a val method to simplify operations. The val() method without parameters returns the value of the current element. The val(values) method is used to set the value of the current element to values. If values ​​is an array, it is more interesting. It is used to match the values ​​in the select element. The values ​​included in values ​​will become selected. .
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 Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

How to change the Microsoft Edge browser to open with 360 navigation - How to change the opening with 360 navigation How to change the Microsoft Edge browser to open with 360 navigation - How to change the opening with 360 navigation Mar 04, 2024 pm 01:50 PM

How to change the page that opens the Microsoft Edge browser to 360 navigation? It is actually very simple, so now I will share with you the method of changing the page that opens the Microsoft Edge browser to 360 navigation. Friends in need can take a look. I hope Can help everyone. Open the Microsoft Edge browser. We see a page like the one below. Click the three-dot icon in the upper right corner. Click "Settings." Click "On startup" in the left column of the settings page. Click on the three points shown in the picture in the right column (do not click "Open New Tab"), then click Edit and change the URL to "0" (or other meaningless numbers). Then click "Save". Next, select "

How to use PUT request method in jQuery? How to use PUT request method in jQuery? Feb 28, 2024 pm 03:12 PM

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

How to set up Cheat Engine in Chinese? Cheat Engine setting Chinese method How to set up Cheat Engine in Chinese? Cheat Engine setting Chinese method Mar 13, 2024 pm 04:49 PM

CheatEngine is a game editor that can edit and modify the game's memory. However, its default language is non-Chinese, which is inconvenient for many friends. So how to set Chinese in CheatEngine? Today, the editor will give you a detailed introduction to how to set up Chinese in CheatEngine. I hope it can help you. Setting method one: 1. Double-click to open the software and click "edit" in the upper left corner. 2. Then click “settings” in the option list below. 3. In the opened window interface, click "languages" in the left column

Where to set the download button in Microsoft Edge - How to set the download button in Microsoft Edge Where to set the download button in Microsoft Edge - How to set the download button in Microsoft Edge Mar 06, 2024 am 11:49 AM

Do you know where to set the download button to display in Microsoft Edge? Below, the editor will bring you the method to set the download button to display in Microsoft Edge. I hope it will be helpful to you. Let’s follow the editor to learn it! Step 1: First open Microsoft Edge Browser, click the [...] logo in the upper right corner, as shown in the figure below. Step 2: Then click [Settings] in the pop-up menu, as shown in the figure below. Step 3: Then click [Appearance] on the left side of the interface, as shown in the figure below. Step 4: Finally, click the button on the right side of [Show Download Button] and it will change from gray to blue, as shown in the figure below. The above is where the editor brings you how to set up the download button in Microsoft Edge.

jQuery Tips: Quickly modify the text of all a tags on the page jQuery Tips: Quickly modify the text of all a tags on the page Feb 28, 2024 pm 09:06 PM

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: &lt

Use jQuery to modify the text content of all a tags Use jQuery to modify the text content of all a tags Feb 28, 2024 pm 05:42 PM

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? How to tell if a jQuery element has a specific attribute? Feb 29, 2024 am 09:03 AM

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

Understand the role and application scenarios of eq in jQuery Understand the role and application scenarios of eq in jQuery Feb 28, 2024 pm 01:15 PM

jQuery is a popular JavaScript library that is widely used to handle DOM manipulation and event handling in web pages. In jQuery, the eq() method is used to select elements at a specified index position. The specific usage and application scenarios are as follows. In jQuery, the eq() method selects the element at a specified index position. Index positions start counting from 0, i.e. the index of the first element is 0, the index of the second element is 1, and so on. The syntax of the eq() method is as follows: $("s

See all articles
1
2