码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2016-06-22 01:42:29      阅读:269      评论:0      收藏:0      [点我收藏+]

标签:

取得所有匹配元素的内容:结果是由所有匹配元素包含的文本内容组合起来的文本,这个方法对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属性

标签:

原文地址:http://www.cnblogs.com/mysearchblog/p/5605571.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!