jQuery - 设置内容和属性 设置内容 - text()、html() 以及 val() , 设置属性 - attr()

jQuery - 设置内容和属性  设置内容 - text()、html() 以及 val()

 

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

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

实例

$("#btn1").click(function(){
  $("#test1").text("Hello world!");
});
$("#btn2").click(function(){
  $("#test2").html("<b>Hello world!</b>");
});
$("#btn3").click(function(){
  $("#test3").val("Dolly Duck");
});


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

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

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

实例

$("button").click(function(){
  $("#w3s").attr({
    "href" : "http://www.w3school.com.cn/jquery",
    "title" : "W3School jQuery Tutorial"
  });
});
posted on 2015-06-09 10:39  软件开发学习分享  阅读(350)  评论(0)    收藏  举报