The difference between html(), text(), and val() in jquery

.html() is used to read and modify the HTML tags of elements, corresponding to innerHTML in js

 .html() is used to read the HTML content of the element (including its Html tag). When the .html() method is used on multiple elements, only the first element is read

 

.text() is used to read or modify the plain text content of the element, corresponding to innerText in js

  text() is used to read the plain text content of an element, including its descendants; the .text() method cannot be used on form elements

 

.val() is used to read or modify the value of a form element

    .val() is used to read the "value" value of the form element, .val() can only be used on form elements

 

 

The difference between the three

   1. The .val() method is the same as .html(), if it is applied to multiple elements, it can only read the "value" value of the first form element, but .text() is different from them, if When .text() is applied to multiple elements, the text content of all selected elements will be read.

    2. .html(), .text(), .val() can use the return value of the callback function to dynamically change the content of multiple elements.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326476343&siteId=291194637