jquery changes dom value

王林
Release: 2023-05-25 09:49:37
Original
669 people have browsed it

In the process of web development, we usually need to add, delete, modify and check DOM elements. Among them, changing the value of DOM elements is a common operation. jQuery, a popular JavaScript library, provides a rich API to change the value of DOM elements.

This article will introduce how jQuery changes DOM values, including how to get the value of an element, how to modify the value of an element, and how to operate multiple elements through chain calls.

Get the value of the element

In jQuery, we can get the DOM element through the selector, and then get the value of the element through the API. The following are some common APIs for getting element values:

  1. val(): Get the value of an input, select, or textarea element.

For example, get the value of a text box:

var value = $('#input-id').val();
Copy after login
  1. text(): Get the text content of the element.

For example, get the text content of a p element:

var text = $('p').text();
Copy after login
  1. html(): Get the HTML content of the element.

For example, to get the HTML content of a div element:

var html = $('div').html();
Copy after login
  1. attr(): Get the attribute value of the element.

For example, to get the src attribute of an img element:

var src = $('img').attr('src');
Copy after login

The above API can also accept a function as a parameter to customize the value of the element. For example:

var value = $('input').val(function(index, value) { return value.toUpperCase(); });
Copy after login

When getting the values of multiple elements, you can use the each() function to traverse the elements and get their values. For example, get the value of all text boxes:

$('input[type="text"]').each(function() { console.log($(this).val()); });
Copy after login

Modify the value of the element

Getting the value of the element is only part of the operation of changing the DOM element. The more common one is to modify the value of the DOM element. Here are some common APIs for modifying DOM elements:

  1. val(): Sets the value of an input, select, or textarea element.

For example, set the value of a text box to "hello":

$('#input-id').val('hello');
Copy after login
  1. text(): Set the text content of the element.

For example, to set the text content of a p element to "world":

$('p').text('world');
Copy after login
  1. html(): Set the HTML content of the element.

For example, set the HTML content of a div element to "

title

":

$('div').html('

title

');
Copy after login
  1. attr(): Set the attribute of the element value.

For example, set the src attribute of an img element to "image.jpg":

$('img').attr('src', 'image.jpg');
Copy after login

Operation on multiple elements

Now, we have understood Learn how to get and modify the value of a single DOM element. But usually, we need to perform the same or different operations on multiple elements. jQuery provides chained calls and filters to operate on multiple elements.

Using chained calls

JQuery's chained calls allow us to operate on multiple elements in one statement. For example, change the background color of all h1 elements to red:

$('h1').css('background-color', 'red');
Copy after login

Set the value of all text boxes to empty:

$('input[type="text"]').val('');
Copy after login

Modify multiple attributes at the same time:

$('img').attr({ 'src': 'image.jpg', 'alt': 'image' });
Copy after login

Using filters

A filter is a powerful selector provided by jQuery that can select a subset of a set of elements. For example, select all elements with class "active":

$('.active')
Copy after login

Select the first p element:

$('p:first')
Copy after login

Select the first three div elements:

$('div:lt(3)')
Copy after login

You can filter The handler is used in combination with the API to operate on the selected elements. For example, set the value of the text box with class "highlight" to empty:

$('input.highlight:text').val('');
Copy after login

The above examples show different filter usages. By familiarizing yourself with the use of filters, you can flexibly select the elements that need to be operated. .

Summary

Through the introduction of this article, we have learned how jQuery obtains and modifies the value of DOM elements, and how to operate multiple elements through chain calls and filters. These techniques allow us to operate DOM elements more quickly and improve development efficiency. At the same time, learning how to operate jQuery can also lay the foundation for learning other JavaScript libraries and frameworks.

The above is the detailed content of jquery changes dom value. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!