hasClass用法

hasClass:检查当前元素是否含有某个特定的类,如果有,就返回true。

下载本地,做外部链接:<script type="text/javascript" src="jquery-1.7.2.min.js"></script>

HTML结构:

 <div class="protected"></div><div class="er"></div>

CSS代码:

 //这里注意div容器要设置绝对定位!
.protected
{ width:100px; height:100px; background:#ff0; position:absolute; z-index:50; }
.er { width:100px; height:100px; background:#f00; position:absolute; z-index:50; top:200px; }

jQuery代码:

 <script type="text/javascript">
      $(document).ready(function(){
        $("div").click(function(){
              if ($(this).hasClass("protected"))
            {
             $(this)
               .animate({ left: -10 })
               .animate({ left: 30 })
               .animate({ left: -10 })
               .animate({ left: 30 })
               .animate({ left: 0 });
            }
        });
    })
 </script>

 

 

 

posted @ 2012-08-30 11:31  奇闻天下  阅读(451)  评论(0)    收藏  举报