【学亮IT手记】jQuery text()/html()回调函数实例

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4 <meta charset="utf-8">
 5 <script src="https://cdn.bootcss.com/jquery/1.10.2/jquery.min.js"></script>
 6 <script type="text/javascript">
 7     $(function(){
 8         $("#btn1").click(function(){
 9             $("#test1").text(function(i,oldVal){
10                 return "旧文本:"+oldVal+" 新文本:你我他学习吧(index:"+i+")";
11             })
12         })
13         $("#btn2").click(function(){
14             $("#test2").html(function(i,oldHtml){
15                 return "旧html:"+oldHtml+" 新html:你我他<b>学习吧</b>(index:"+i+")";
16             })
17         })
18     })
19 </script>
20 </head>
21 
22 <body>
23     <p id="test1">这是段落中的<b>粗体</b>文本。</p>
24     <p id="test2">这是另一个有<b>粗体</b>字的段落。</p>
25     <button id="btn1">显示 新/旧 文本</button>
26     <button id="btn2">显示 新/旧 HTML</button>
27 </body>
28 </html>

猜你喜欢

转载自www.cnblogs.com/niwotaxuexiba/p/9460658.html