站长资讯网
最全最丰富的资讯网站

jquery怎样判断属性是否存在

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

jquery怎样判断属性是否存在

本教程操作环境: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视频教程

赞(0)
分享到: 更多 (0)