jQuery - 设置内容和属性

  • text() - 设置或返回所选元素的文本内容
  • html() - 设置或返回所选元素的内容(包括 HTML 标记)
  • val() - 设置或返回表单字段的值

下面的例子演示如何通过 text()、html() 以及 val() 方法来设置内容:

实例

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4 <script src="/jquery/jquery-1.11.1.min.js"></script>
 5 <script>
 6 $(document).ready(function(){
 7   $("#btn1").click(function(){
 8     $("#test1").text("Hello world!");
 9   });
10   $("#btn2").click(function(){
11     $("#test2").html("<b>Hello world!</b>");
12   });
13   $("#btn3").click(function(){
14     $("#test3").val("Dolly Duck");
15   });
16 });
17 </script>
18 </head>
19 
20 <body>
21 <p id="test1">这是段落。</p>
22 <p id="test2">这是另一个段落。</p>
23 <p>Input field: <input type="text" id="test3" value="Mickey Mouse"></p>
24 <button id="btn1">设置文本</button>
25 <button id="btn2">设置 HTML</button>
26 <button id="btn3">设置值</button>
27 </body>
28 </html>
View Code

查看结果:

默认:                          点设置文本:

      

再点设置HTML:                      再点设置值:

        

 


 

 

text()、html() 以及 val() 的回调函数

上面的三个 jQuery 方法:text()、html() 以及 val(),同样拥有回调函数。回调函数由两个参数:被选元素列表中当前元素的下标,以及原始(旧的)值。然后以函数新值返回您希望使用的字符串。

下面的例子演示带有回调函数的 text() 和 html():

实例

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4 <script src="/jquery/jquery-1.11.1.min.js"></script>
 5 <script>
 6 $(document).ready(function(){
 7   $("#btn1").click(function(){
 8     $("#test1").text(function(i,origText){
 9       return "Old text: " + origText + " New text: Hello world! (index: " + i + ")"; 
10     });
11   });
12 
13   $("#btn2").click(function(){
14     $("#test2").html(function(i,origText){
15       return "Old html: " + origText + " New html: Hello <b>world!</b> (index: " + i + ")"; 
16     });
17   });
18 
19 });
20 </script>
21 </head>
22 
23 <body>
24 <p id="test1">这是<b>粗体</b>文本。</p>
25 <p id="test2">这是另一段<b>粗体</b>文本。</p>
26 <button id="btn1">显示旧/新文本</button>
27 <button id="btn2">显示旧/新 HTML</button>
28 </body>
29 </html>
View Code

查看结果:


 

设置属性 - attr()

jQuery attr() 方法也用于设置/改变属性值。

下面的例子演示如何改变(设置)链接中 href 属性的值:

实例

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4 <script src="/jquery/jquery-1.11.1.min.js"></script>
 5 <script>
 6 $(document).ready(function(){
 7   $("button").click(function(){
 8     $("#w3s").attr("href","http://www.w3school.com.cn/jquery");
 9   });
10 });
11 </script>
12 </head>
13 
14 <body>
15 <p><a href="http://www.w3school.com.cn" id="w3s">W3School.com.cn</a></p>
16 <button>改变 href 值</button>
17 <p>请把鼠标指针移动到链接上,或者点击该链接,来查看已经改变的 href 值。</p>
18 </body>
19 </html>
View Code

查看结果:

默认:

 

点击改变href值之后,点击链接:

 


 

attr() 方法也允许您同时设置多个属性。

下面的例子演示如何同时设置 href 和 title 属性:

实例

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4 <script src="/jquery/jquery-1.11.1.min.js"></script>
 5 <script>
 6 $(document).ready(function(){
 7   $("button").click(function(){
 8     $("#w3s").attr({
 9       "href" : "http://www.w3school.com.cn/jquery/",
10       "title" : "W3School jQuery 教程"
11     });
12   });
13 });
14 </script>
15 </head>
16 
17 <body>
18 <p><a href="http://www.w3school.com.cn" id="w3s">W3School.com.cn</a></p>
19 <button>改变 href 和 title 值</button>
20 <p>请把鼠标指针移动到链接上,或者点击该链接,来查看已经改变的 href 值和已经设置的 title 值。</p>
21 </body>
22 </html>
View Code

attr() 的回调函数

jQuery 方法 attr(),也提供回调函数。回调函数由两个参数:被选元素列表中当前元素的下标,以及原始(旧的)值。然后以函数新值返回您希望使用的字符串。

下面的例子演示带有回调函数的 attr() 方法:

实例

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4 <script src="/jquery/jquery-1.11.1.min.js"></script>
 5 <script>
 6 $(document).ready(function(){
 7   $("button").click(function(){
 8     $("#w3s").attr("href", function(i,origValue){
 9       return origValue + "/jquery"; 
10     });
11   }); 
12 });
13 </script>
14 </head>
15 
16 <body>
17 <p><a href="http://www.w3school.com.cn" id="w3s">w3school.com.cn</a></p>
18 <button>改变 href 值</button>
19 <p>请把鼠标指针移动到链接上,或者点击该链接,来查看已经改变的 href 值。</p>
20 </body>
21 </html>
View Code

 

posted @ 2016-03-30 15:13  司会铭  阅读(1217)  评论(0)    收藏  举报