jQuery设置内容和属性方

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

示例:

 <!DOCTYPE html>
 <html>
 <head>
 <script src="/jquery/jquery-1.11.1.min.js"></script>
 <script>
 $(document).ready(function(){
   $("#btn1").click(function(){
     $("#test1").text("Hello world!");
   });
   $("#btn2").click(function(){
     $("#test2").html("<b>Hello world!</b>");
   });
   $("#btn3").click(function(){
     $("#test3").val("Dolly Duck");
   });
 });
 </script>
 </head>
 
 <body>
 <p id="test1">这是段落。</p>
 <p id="test2">这是另一个段落。</p>
 <p>Input field: <input type="text" id="test3" value="Mickey Mouse"></p>
 <button id="btn1">设置文本</button>
 <button id="btn2">设置 HTML</button>
 <button id="btn3">设置值</button>
 </body>
 </html>

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

 <!DOCTYPE html>
 <html>
 <head>
 <script src="jquery-2.2.0.min.js"></script>
 <script>
 $(document).ready(function(){
   $("#btn1").click(function(){
     $("#test1").text(function(i,origText){
       return "Old text: " + origText + " New text: Hello world! (index: " + i + ")"; 
     });
   });
 
   $("#btn2").click(function(){
     $("#test2").html(function(i,origText){
       return "Old html: " + origText + " New html: Hello <b>world!</b> (index: " + i + ")"; 
     });
   });
 
 });
 </script>
 </head>
 
 <body>
 <p id="test1">这是<b>粗体</b>文本。</p>
 <p id="test2">这是另一段<b>粗体</b>文本。</p>
 <button id="btn1">显示旧/新文本</button>
 <button id="btn2">显示旧/新 HTML</button>
 </body>
 </html>

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

 <!DOCTYPE html>
 <html>
 <head>
 <script src="/jquery/jquery-1.11.1.min.js"></script>
 <script>
 $(document).ready(function(){
   $("button").click(function(){
     $("#w3s").attr("href","http://hovertree.com/jquery");
   });
 });
 </script>
 </head>
 
 <body>
 <p><a href="http://hovertree.com" id="w3s">hovertree.com.cn</a></p>
 <button>改变 href 值</button>
 <p>请把鼠标指针移动到链接上,或者点击该链接,来查看已经改变的 href 值。</p>
 </body>
 </html>

jquery中用attr()方法来获取和设置元素属性,attr是attribute(属性)的缩写,在jQuery DOM操作中会经常用到attr(),attr()有4个表达式。

1. attr(属性名) //获取属性的值(取得第一个匹配元素的属性值。通过这个方法可以方便地从第一个匹配元素中获取一个属性的值。如果元素没有相应属性,则返回 undefined )

2. attr(属性名, 属性值) //设置属性的值 (为所有匹配的元素设置一个属性值。)

3. attr(属性名,函数值) //设置属性的函数值 (为所有匹配的元素设置一个计算的属性值。不提供值,而是提供一个函数,由这个函数计算的值作为属性值。)

4.attr(properties) //给指定元素设置多个属性值,即:{属性名一: “属性值一” , 属性名二: “属性值二” , … … }。(这是一种在所有匹配元素中批量设置很多属性的最佳方式。 注意,如果你要设置对象的class属性,你必须使用'className' 作为属性名。或者你可以直接使用'class'或者'id'。示例如下:)

 $("button").click(function(){
   $("#w3s").attr({
     "href" : "http://hovertree.com/jquery",
     "title" : "hovertree jQuery Tutorial"
   });
 });

attr() 的回调函数
jQuery 方法 attr(),也提供回调函数。

 <!DOCTYPE html>
 <html>
 <head>
 <script src="/jquery/jquery-1.11.1.min.js"></script>
 <script>
 $(document).ready(function(){
   $("button").click(function(){
     $("#w3s").attr("href", function(i,origValue){
       return origValue + "/jquery"; 
     });
   }); 
 });
 </script>
 </head>
 
 <body>
 <p><a href="http://hovertree.com" id="w3s">hovertree.com.cn</a></p>
 <button>改变 href 值</button>
 <p>请把鼠标指针移动到链接上,或者点击该链接,来查看已经改变的 href 值。</p>
 </body>
 </html>
原文地址:https://www.cnblogs.com/double405/p/5174838.html