[ jquery 方法 text([val|fn]) ] 此方法操作匹配的元素或元素集中的内容,相当于javascript中的innerText属性

Posted 窗棂博客记录

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[ jquery 方法 text([val|fn]) ] 此方法操作匹配的元素或元素集中的内容,相当于javascript中的innerText属性相关的知识,希望对你有一定的参考价值。

取得所有匹配元素的内容:结果是由所有匹配元素包含的文本内容组合起来的文本,这个方法对html和XML文档都有效,相当于javascript中的innerText属性,可读可写,但是无法解析HTML标签:

如果传入的是callback,此函数返回一个字符串并且参数解析如下:

  1.index为元素在集合中的索引位置

  2.text为原先的text值。

实例:

<!DOCTYPE html>
<html lang=‘zh-cn‘>
<head>
<title>Insert you title</title>
<meta http-equiv=‘description‘ content=‘this is my page‘>
<meta http-equiv=‘keywords‘ content=‘keyword1,keyword2,keyword3‘>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<!-- <script type=‘text/javascript‘ src=‘./js/jquery-3.0.0.js‘></script> -->
<script type=‘text/javascript‘ src=‘./js/jquery-1.12.1.min.js‘></script>
<style type=‘text/css‘>
   div{font:400 25px/250px ‘Courier New‘;text-align:center;color:#000;border:1px solid #F50;width:700px;height:250px;margin:20px auto;}
   .addOne{padding:0 8px;}
   .addTwo{color:#999;}
</style>
<script type=‘text/javascript‘>
    $(function(){
            alert($(‘#user‘).text());
           $(‘#user‘).text(function(index,oldText){
               return oldText + ‘<strong>this is a test</strong>‘;
           });
    });
</script>
</head>
<body>
   <div id=‘user‘ class=‘addOne addTwo‘>oldChar</div>
</body>
</html>

 

以上是关于[ jquery 方法 text([val|fn]) ] 此方法操作匹配的元素或元素集中的内容,相当于javascript中的innerText属性的主要内容,如果未能解决你的问题,请参考以下文章

jQuery学习手册(14)

[ jquery 方法val([val|fn|arr]) ] 此方法获得匹配元素的当前值,常应用于表单元素,select....

[ jquery 方法 html([val|fn]) ] 此方法操作匹配的元素或元素集中的内容,相当于javascript中的innerHTML

详说jQuery的 val() 事件

如何覆盖 jQuery 插件的 .val()?

jquery属性操作之html,text,val方法