1.獲取p元素的title屬性:
var title = $("p").attr("title");
2.給p元素加title屬性(值為:栗子)和date屬性(值為:2019/7/15):
$("p").attr("title":"栗子", "date":"2019/7/15");
3.刪除p中的title屬性:
$("p").removeAttr("title");
4.給p添加class屬性(值為:another):
$("p").attr("class":"anthor");
// 追加一個high樣式
$("p").addClass("high");
5.刪除樣式high和another:
$("p").removeClass("high another");
// 去除掉所有的樣式
$("p").removeClass();
6.獲取p的html元素:
<p title="選擇你喜歡的水果."><strong>你喜歡的水果是?</strong></p>
var p_html = $("p").html();
console.log(p_html); // <strong>你喜歡的水果是?</strong>
7.獲取p的文本內容:
var p_text = $("p").text();
console.log(p_text); // 你喜歡的水果是
8.獲取id為address的input框的value值:
// html
<input type="text" id="address" value="請輸入郵箱地址" />//js
var txt_val = $('#address').val();
console.log(txt_val); // 請輸入郵箱地址// 給Input框賦值:
$('#address').val('hello jquery!');
獲取焦點/失去焦點:
// 獲取焦點
$('#address').focus(function(){console.log("focus");
})
// 失去焦點
$('#address').blur(function (){console.log("blur");
})
參考《鋒利的jQuery》(第2版)P80~P82