4.2 屬性(Attributes)的操控

.attr():取得屬性值與修改

取得屬性 id 的屬性值:

$("#p1").attr("id");

將屬性 id 的屬性值設定成 another_id:

$("#p1").attr("id", "another_id");

一次設定多個屬性:

$("a#link2").attr("href", "https://www.momoshop.com.tw/main/Main.jsp").attr("style", "color: red;font-size: 30px;");

或:

$("a#link2").attr({
  "href": "https://www.momoshop.com.tw/main/Main.jsp",
  "style": "color: red;font-size: 30px;"
});

範例:

.removeAttr():移除屬性

移除 style 屬性:

$("a#link2").removeAttr("style");

範例:

Last updated