एक jQuery hasAttr () समतुल्य - सीएसएस-ट्रिक्स

Anonim

jQuery के पास वास्तव में एक .hasAttr()फ़ंक्शन नहीं है। आप मान सकते हैं कि यह करता है, लेकिन अफसोस, ऐसा नहीं है।

एक StackOverflow धागे में कुछ बहुत अच्छे समाधान हैं।

विशेषता प्राप्त करें, मान जांचें

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) ( // Element has this attribute )

देशी जावास्क्रिप्ट का एक तरीका है

यदि आपके पास केवल एक jQuery संदर्भ है ...

$(this)(0).hasAttribute("name"); jQObject(0).hasAttribute("name");

चयन को फ़िल्टर करें

$(this).is('(name)'); $(this).filter("(name='choice')");