jquery怎样判断属性是否存在

来自:互联网
时间:2020-12-25
阅读:

jquery判断属性是否存在的方法:

jquery判断属性存在可以使用attr()方法、is()方法、filter()方法,以及原生js的hasAttribute()方法。

1、使用attr()获取属性,检查值的类型

var attr = $(this).attr('name');
// 对于一些浏览器,attr是undefined、或者是false
if (typeof attr !== typeof undefined && attr !== false) {
  alert('具有该属性')
}

2、原生js的hasAttribute方法

$(this)[0].hasAttribute("name");
jQObject[0].hasAttribute("name");

3、过滤选择的方式

$(this).is('[name]');
$(this).filter("[name='choice']");
返回顶部
顶部