Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
541 views
in Technique[技术] by (71.8m points)

jquery - jQuery hasAttr检查元素是否有属性[重复](jQuery hasAttr checking to see if there is an attribute on an element [duplicate])

Possible Duplicate:

(可能重复:)
Check existence of an attribute with JQuery

(使用JQuery检查属性是否存在)

How do you check if there is an attribute on an element in jQuery?

(你如何检查jQuery中的元素是否有属性?)

Similar to hasClass , but with attr ?

(与hasClass类似,但是attr ?)

For example,

(例如,)

if ($(this).hasAttr("name")) {
    // ...
}
  ask by Mark translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
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) {
    // ...
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...