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、或者是falseif (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怎样判断属性是否存在的详细内容。