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
}
本机JavaScript有一种方法
如果您只有jQuery参考...
$(this)[0].hasAttribute("name");
jQObject[0].hasAttribute("name");
筛选选择
$(this).is('[name]');
$(this).filter("[name='choice']");
翻译自: https://css-tricks.com/snippets/jquery/make-an-jquery-hasattr/