最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

jquery学习之二属性文本与值(text,val)_jquery

来源:动视网 责编:小采 时间:2020-11-27 20:55:56
文档

jquery学习之二属性文本与值(text,val)_jquery

jquery学习之二属性文本与值(text,val)_jquery:text() 取得所有匹配元素的内容。 结果是由所有匹配元素包含的文本内容组合起来的文本。这个方法对HTML和XML文档都有效。 Get the text contents of all matched elements. The result is a string that contains the
推荐度:
导读jquery学习之二属性文本与值(text,val)_jquery:text() 取得所有匹配元素的内容。 结果是由所有匹配元素包含的文本内容组合起来的文本。这个方法对HTML和XML文档都有效。 Get the text contents of all matched elements. The result is a string that contains the


text()

取得所有匹配元素的内容。 结果是由所有匹配元素包含的文本内容组合起来的文本。这个方法对HTML和XML文档都有效。 Get the text contents of all matched elements. The result is a string that contains the combined text contents of all matched elements. This method works on both HTML and XML documents.

返回值

String

示例

HTML 代码:

Test Paragraph.

Paraparagraph

jQuery 代码:

$("p").text();

结果:

Test Paragraph.Paraparagraph ------------------------------------------------------------------------------------------------------------------------------

text(val)

设置所有匹配元素的文本内容 与 html() 类似, 但将编码 HTML (将 "<" 和 ">" 替换成相应的HTML实体). Set the text contents of all matched elements. Similar to html(), but escapes HTML (replace "<" and ">" with their HTML entities).

返回值

jQuery

参数

val (String) : 用于设置元素内容的文本

示例

HTML 代码:

Test Paragraph.

jQuery 代码:

$("p").text("Some new text.");

结果:

[

Some new text.

] ------------------------------------------------------------------------------------------------------------------------------

val()

获得第一个匹配元素的当前值。 在 jQuery 1.2 中,可以返回任意元素的值了。包括select。如果多选,将返回一个数组,其包含所选的值。 Get the content of the value attribute of the first matched element. In jQuery 1.2, a value is now returned for all elements, including selects. For multiple selects an array of values is returned.

返回值

String,Array

示例

获得单个select的值和多选select的值。

HTML 代码:




jQuery 代码:

$("p").append(
"Single: " + $("#single").val() +
" Multiple: " + $("#multiple").val().join(", ")
);

结果:

[

Single:SingleMultiple:Multiple, Multiple3

]

获取文本框中的值

HTML 代码:

jQuery 代码:

$("input").val();

结果:

some text ------------------------------------------------------------------------------------------------------------------------------

val(val)

设置每一个匹配元素的值。 在 jQuery 1.2, 这也可以为select元件赋值 Set the value attribute of every matched element. In jQuery 1.2, this is also able to set the value of select elements, but selecting the appropriate options.

返回值

jQuery

参数

val (String) : 要设置的值。

示例

设定文本框的值

HTML 代码:

jQuery 代码:

$("input").val("hello world!"); ------------------------------------------------------------------------------------------------------------------------------

val(val)

check,select,radio等都能使用为之赋值

返回值

jQuery

参数

val (Array) : 用于 check/select 的值

示例

设定一个select和一个多选的select的值

HTML 代码:




check1
check2
radio1
radio2

jQuery 代码:

$("#single").val("Single2");
$("#multiple").val(["Multiple2", "Multiple3"]);
$("input").val(["check2", "radio1"]);

文档

jquery学习之二属性文本与值(text,val)_jquery

jquery学习之二属性文本与值(text,val)_jquery:text() 取得所有匹配元素的内容。 结果是由所有匹配元素包含的文本内容组合起来的文本。这个方法对HTML和XML文档都有效。 Get the text contents of all matched elements. The result is a string that contains the
推荐度:
标签: 文本 和属性 jQuery
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top