3 回答

TA貢獻(xiàn)1825條經(jīng)驗(yàn) 獲得超4個(gè)贊
您可以致電.prop("tagName")。例子:
jQuery("<a>").prop("tagName"); //==> "A"
jQuery("<h1>").prop("tagName"); //==> "H1"
jQuery("<coolTagName999>").prop("tagName"); //==> "COOLTAGNAME999"
如果寫(xiě)出來(lái).prop("tagName")很麻煩,則可以創(chuàng)建一個(gè)自定義函數(shù),如下所示:
jQuery.fn.tagName = function() {
return this.prop("tagName");
};
例子:
jQuery("<a>").tagName(); //==> "A"
jQuery("<h1>").tagName(); //==> "H1"
jQuery("<coolTagName999>").tagName(); //==> "COOLTAGNAME999"
請(qǐng)注意,按照慣例,標(biāo)簽名稱返回CAPITALIZED。如果希望返回的標(biāo)簽名稱全部為小寫(xiě)字母,則可以編輯自定義函數(shù),如下所示:
jQuery.fn.tagNameLowerCase = function() {
return this.prop("tagName").toLowerCase();
};
例子:
jQuery("<a>").tagNameLowerCase(); //==> "a"
jQuery("<h1>").tagNameLowerCase(); //==> "h1"
jQuery("<coolTagName999>").tagNameLowerCase(); //==> "cooltagname999"

TA貢獻(xiàn)1111條經(jīng)驗(yàn) 獲得超0個(gè)贊
jQuery 1.6以上
jQuery('selector').prop("tagName").toLowerCase()
舊版本
jQuery('selector').attr("tagName").toLowerCase()
toLowerCase()不是必需的。
添加回答
舉報(bào)