如何检查jQuery中的元素是否有属性?类似于hasClass,但是attr?例如,if ($(this).hasAttr("name")) {
// ...}
3 回答

有只小跳蛙
TA贡献1824条经验 获得超8个赞
var attr = $(this).attr('name');
// For some browsers, `attr` is undefined; for others,
// `attr` is false. Check for both.
if (typeof attr !== typeof undefined && attr !== false) {
// ...
}

慕的地8271018
TA贡献1796条经验 获得超4个赞
$(this).is("[name]")
?
[attr]
attr
.is()

拉莫斯之舞
TA贡献1820条经验 获得超10个赞
hasAttr
$.fn.hasAttr = function(name) { return this.attr(name) !== undefined;};$(document).ready(function() { if($('.edit').hasAttr('id')) { alert('true'); } else { alert('false'); }});<div class="edit" id="div_1">Test field</div>
- 3 回答
- 0 关注
- 681 浏览
添加回答
举报
0/150
提交
取消