jQuery 入門教程(13): HTML Set

來源:互聯網
上載者:User

text() – 設定或取得指定元素的常值內容。html() – 設定或取得指定元素的內容(包括HTML標記)val() – 設定或取得表單某個輸入欄位的值。比如下面代碼就是使用上面三種方法給HTML元素或Form賦值  [javascript]  $("#btn1").click(function(){     $("#test1").text("Hello world!");   });   $("#btn2").click(function(){     $("#test2").html("<b>Hello world!</b>");   });   $("#btn3").click(function(){     $("#test3").val("Dolly Duck");   });      $("#btn1").click(function(){   $("#test1").text("Hello world!"); }); $("#btn2").click(function(){   $("#test2").html("<b>Hello world!</b>"); }); $("#btn3").click(function(){   $("#test3").val("Dolly Duck"); });   用於text(),html()和val()的回呼函數 text(),html()和val()方法可以和一個回呼函數配合使用,這個回呼函數具有兩個參數,一個是選擇中元素的序號,第二個為當前值(舊值),然後你可以通過回呼函數的傳回值做為元素的新值。例如: [html] <!DOCTYPE html>  <html>  <head>      <meta charset="utf-8">      <title>JQuery Demo</title>      <script src="scripts/jquery-1.9.1.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">This is a <b>bold</b> paragraph.</p>      <p id="test2">This is another <b>bold</b> paragraph.</p>      <button id="btn1">Show Old/New Text</button>      <button id="btn2">Show Old/New HTML</button>  </body>  </html>   <!DOCTYPE html><html><head>    <meta charset="utf-8">    <title>JQuery Demo</title>    <script src="scripts/jquery-1.9.1.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">This is a <b>bold</b> paragraph.</p>    <p id="test2">This is another <b>bold</b> paragraph.</p>    <button id="btn1">Show Old/New Text</button>    <button id="btn2">Show Old/New HTML</button></body></html> 同樣為元素的屬性賦值也使用attr()方法,例如: [javascript]  $("button").click(function(){     $("#guidebee").attr({       "href" : "http://www.imobilebbs.com",       "title" : "imobilebbs jQuery Tutorial"     });   });    $("button").click(function(){   $("#guidebee").attr({     "href" : "http://www.imobilebbs.com",     "title" : "imobilebbs jQuery Tutorial"   }); });   

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.