Home  >  Article  >  Web Front-end  >  用JQuery 判断某个属性是否存在hasAttr的解决方法_jquery

用JQuery 判断某个属性是否存在hasAttr的解决方法_jquery

WBOY
WBOYOriginal
2016-05-16 17:35:111279browse

在JQuery编码中,我们会判断元素是否存在某个属性.比如是否包含 class="new" 的样式呢.JQuery判断就非常简单了,因为有 hasClass这个方法 $("input[name=new]").hasClass("new") 即可判断.

但是有时候我们需要判断别的属性,比如有的 a 链接包含 rel 属性,有的没有rel属性.这时该怎么判断呢?

这时就没有现成的方法了. 如果存在某个属性 $("#aid").attr("rel") 会返回 rel的值,如果不存在 rel属性则会返回"undefined"

undefined 就是 undefined类型 , if($("#aid").attr("rel")=="undefined") 这个判断可能不成立.

因为类型不相同.

建议使用 if(typeof($("#aid").attr("rel"))=="undefined") 即可

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn