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

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

问题描述

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

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

你会在哪里使用一个?

推荐答案

.val() 适用于输入元素(或任何具有 value 属性的元素?)和 .text() 不适用于输入元素..val() 获取输入元素的值——与类型无关..text() 获取所有匹配元素的innerText(不是HTML):

.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()

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

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()

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

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

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

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