Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
102 views
in Technique[技术] by (71.8m points)

javascript - val()和text()之间的区别(Difference between val() and text())

What the difference between jQuery's functions val() and text() ?

(jQuery的函数val()text()之间有什么区别?)

Where would you use one over the other?

(你会在哪里使用一个?)

  ask by Dan Appleyard translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

.val() works on input elements (or any element with a value attribute?) and .text() will not work on input elements.

(.val()适用于输入元素(或任何带有value属性的元素?)和.text()不适用于输入元素。)

.val() gets the value of the input element -- regardless of type.

(.val()获取input元素的值 - 无论类型如何。)

.text() gets the innerText (not HTML) of all the matched elements:

(.text()获取所有匹配元素的innerText(而不是HTML):)

.text()

The result is a string that contains the combined text contents of all matched elements.

(结果是一个字符串,其中包含所有匹配元素的组合文本内容。)

This method works on both HTML and XML documents.

(此方法适用于HTML和XML文档。)

Cannot be used on input elements.

(不能用于输入元素。)

For input field text use the val attribute.

(对于输入字段文本,请使用val属性。)

.val()

Get the content of the value attribute of the first matched element

(获取第一个匹配元素的value属性的内容)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...