7万
861
32万
论坛元老
在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") 即可
使用道具 举报
0
1万
68
注册会员
6
174
2万
194
124
中级会员
2
499
146
100
本版积分规则 发表回复 回帖后跳转到最后一页
手机版|小黑屋|网站地图|源码论坛 ( 海外版 )
GMT+8, 2025-2-5 07:52 , Processed in 0.134087 second(s), 24 queries .
Powered by Discuz! X3.4
Copyright © 2001-2020, Tencent Cloud.