国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 擴展 > jQuery > 正文

jquery 學習之二 屬性 文本與值(text,val)

2024-09-06 20:04:37
字體:
來源:轉載
供稿:網(wǎng)友

取得所有匹配元素的內(nèi)容。
結果是由所有匹配元素包含的文本內(nèi)容組合起來的文本。這個方法對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 代碼:

<p><b>Test</b> Paragraph.</p><p>Paraparagraph</p>
jQuery 代碼:

$("p").text();
結果:

Test Paragraph.Paraparagraph
------------------------------------------------------------------------------------------------------------------------------
text(val)設置所有匹配元素的文本內(nèi)容
與 html() 類似, 但將編碼 HTML (將 "<" 和 ">" 替換成相應的HTML實體).
Set the text contents of all matched elements.
Similar to html(), but escapes HTML (replace "<" and ">" with their HTML entities).
返回值
jQuery

參數(shù)
val (String) : 用于設置元素內(nèi)容的文本

示例
HTML 代碼:

<p>Test Paragraph.</p>
jQuery 代碼:

$("p").text("<b>Some</b> new text.");
結果:

[ <p><b>Some</b> new text.</p> ]
------------------------------------------------------------------------------------------------------------------------------
val()獲得第一個匹配元素的當前值。
在 jQuery 1.2 中,可以返回任意元素的值了。包括select。如果多選,將返回一個數(shù)組,其包含所選的值。
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 代碼:

<p></p><br/>
<select id="single">
<option>Single</option>
<option>Single2</option>
</select>
<select id="multiple" multiple="multiple">
<option selected="selected">Multiple</option>
<option>Multiple2</option>
<option selected="selected">Multiple3</option>
</select>
jQuery 代碼:

$("p").append(
"<b>Single:</b> " + $("#single").val() +
" <b>Multiple:</b> " + $("#multiple").val().join(", ")
);
結果:

[ <p><b>Single:</b>Single<b>Multiple:</b>Multiple, Multiple3</p>]
獲取文本框中的值

HTML 代碼:

<input type="text" value="some text"/>
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

參數(shù)
val (String) : 要設置的值。

示例
設定文本框的值

HTML 代碼:

<input type="text"/>
jQuery 代碼:

$("input").val("hello world!");
------------------------------------------------------------------------------------------------------------------------------
val(val)check,select,radio等都能使用為之賦值
返回值
jQuery

參數(shù)
val (Array<String>) : 用于 check/select 的值

示例
設定一個select和一個多選的select的值

HTML 代碼:

<select id="single">
<option>Single</option>
<option>Single2</option>
</select>
<select id="multiple" multiple="multiple">
<option selected="selected">Multiple</option>
<option>Multiple2</option>
<option selected="selected">Multiple3</option>
</select><br/>
<input type="checkbox" value="check1"/> check1
<input type="checkbox" value="check2"/> check2
<input type="radio" value="radio1"/> radio1
<input type="radio" value="radio2"/> radio2
jQuery 代碼:

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

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 黄梅县| 响水县| 龙海市| 叙永县| 南漳县| 彩票| 新巴尔虎左旗| 格尔木市| 庆阳市| 正阳县| 南川市| 长白| 德阳市| 建宁县| 田林县| 巫溪县| 洞头县| 乾安县| 嘉义市| 宁安市| 富源县| 鄂温| 德惠市| 汤阴县| 兖州市| 兴城市| 福建省| 新源县| 日喀则市| 隆昌县| 光泽县| 时尚| 师宗县| 平顶山市| 和田县| 泗阳县| 浦城县| 金寨县| 钟山县| 龙陵县| 淳安县|