val()和text()之间的区别 [英] Difference between val() and text()

查看:74
本文介绍了val()和text()之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

jQuery的函数 val() text()

$ b $有什么区别b

您会在哪里使用另一个?

解决方案

.val ()对输入元素(或任何具有值属性的元素?)和 .text()不起作用。 .val()获取输入元素的值 - 无论类型如何。 .text()获取所有匹配元素的innerText(不是HTML):




结果是一个字符串,其中包含
所有
匹配元素的组合文本内容。此方法适用于
HTML和XML文档。输入元素不能使用
。对于输入
字段文本,请使用val属性。


.val()


获取第一个匹配元素的值属性
的内容



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

Where would you use one over the other?

解决方案

.val() works on input elements (or any element with a value attribute?) and .text() will not work on input elements. .val() gets the value of the input element -- regardless of type. .text() gets the innerText (not HTML) of all the matched elements:

.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. Cannot be used on input elements. For input field text use the val attribute.

.val()

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

这篇关于val()和text()之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆