jQuery hasAttr检查元素上是否有属性 [英] jQuery hasAttr checking to see if there is an attribute on an element

查看:101
本文介绍了jQuery hasAttr检查元素上是否有属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
使用JQuery检查属性是否存在

Possible Duplicate:
Check existence of an attribute with JQuery

如何检查jQuery元素中是否有属性?与hasClass类似,但具有attr?

How do you check if there is an attribute on an element in jQuery? Similar to hasClass, but with attr?

例如

if ($(this).hasAttr("name")) {
    // ...
}

推荐答案

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) {
    // ...
}

这篇关于jQuery hasAttr检查元素上是否有属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆