Home  >  Article  >  Web Front-end  >  The difference between text(), html() and val() in js

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

高洛峰
高洛峰Original
2016-10-09 13:51:191135browse

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

text(), html() and val() are used to store and get values ​​of html elements, but they each have their own differences Features: text() is used to access the text content of html elements. html() can be used not only to access the text content of html elements, but also to access html content. val() is used to access the content of the input element.

一. text()

var text = $("div").text();

console.log(text);

text

console:text

 

二.html( )

text

var str = $("div").html();

console.log(str);

console:

text

three. val()

var str = $("input").val();

console.log(str);

console:text

Similarities and differences:

text() and html() can both be used for element text Access, but html() can be used not only for accessing element text, but also for accessing element nodes.

val() is different from text() and html(), val() is used to access the content of the input node.


Statement:
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