小白教程
所有教程
关于
Search
172.71.254.8
172.71.254.8
参数设置
贡献
退出
操作
编辑
移动
保护
信息
历史
删除
查看“Html text”的源代码
本页内容
上一节:
Css_scrolltop
下一节:
Html_toggleclass
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
{{DISPLAYTITLE:text()}}[[Category:jquery ref html|34]] = jQuery text() 方法 = [[jquery ref html|jQuery HTML/CSS 方法]] 设置所有 <p> 元素的文本内容: <sample title="" desc="" lang="html" hererun="1"> $("button").click(function(){ $("p").text("Hello world!"); }); </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"> </script> <script> $(document).ready(function(){ $("button").click(function(){ $("p").text("Hello world!"); }); }); </script> </head> <body> <button>设置所有p元素的文本内容</button> <p>这是一个段落。</p> <p>这是另一个段落。</p> </body> </html> </run> == 定义和用法 == text() 方法设置或返回被选元素的文本内容。 当该方法用于'''返回'''内容时,则返回所有匹配元素的文本内容(会删除 HTML 标记)。 当该方法用于'''设置'''内容时,则重写所有匹配元素的内容。 '''提示:'''如需设置或返回被选元素的 innerHTML(文本 + HTML 标记),请使用 [[html html|html()]] 方法。 == 语法 == 返回文本内容: $(selector).text() 设置文本内容: $(selector).text(content) 使用函数设置文本内容: $(selector).text(function(index,currentcontent)) {| class="table table-striped table-hover" !width="50%"| 参数 !width="50%"| 描述 |- | content | 必需。规定被选元素的新文本内容。'''注意:''' 特殊字符会被编码。 |- | function(index,currentcontent) | 可选。规定返回被选元素的新文本内容的函数。 * index - 返回集合中元素的 index 位置。 * currentcontent - 返回被选元素的当前内容。 |} == 更多实例 == [[try.php?filename=tryjquery_html_text_get|返回文本内容]] 如何返回被选元素的文本内容。 [[try.php?filename=tryjquery_html_text_func|使用函数设置文本内容]] 使用函数设置被选元素的文本内容。 [[jquery ref html|jQuery HTML/CSS 方法]]
返回至“
Html text
”。
上一节:
Css_scrolltop
下一节:
Html_toggleclass