您的位置:首页 > 技术中心 > 前端框架 >

jquery怎样判断属性是否存在

时间:2020-12-25 15:00

jquery判断属性是否存在的方法:1、使用【attr()】获取属性,检查值的类型;2、原生js的hasAttribute方法;3过滤选择的方式,代码为【$(this).is('[name]');】。

本教程操作环境:windows7系统、jquery3.2.1版本,DELL G3电脑。

推荐:jquery视频教程

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']");

相关免费学习推荐:js视频教程

以上就是jquery怎样判断属性是否存在的详细内容,更多请关注www.gxlsystem.com其它相关文章!

本类排行

今日推荐

热门手游