首页 > 语言 > JavaScript > 正文

jQuery判断一个元素是否可见的方法

2024-05-06 16:21:33
字体:
来源:转载
供稿:网友

这篇文章主要介绍了jQuery判断一个元素是否可见的方法,涉及jQuery链式操作及样式判定的技巧,需要的朋友可以参考下

本文实例讲述了jQuery判断一个元素是否可见的方法。分享给大家供大家参考。具体如下:

jQuery 可以很容易地确定一个元素是可见的或是隐藏的,然后分别做不同的处理。如:我想根据某 div 是否可见,在按钮上显示不同的文字和图标。可以这样实现:

方法一:

 

 
  1. $('#para_div button').click(function() {  
  2. if($(this).next().is(":visible")) {  
  3. //$(this).html('显示');  
  4. $(this).css({"background":"url(/images/btn_arrow_down.png) no-repeat"});  
  5. }  
  6. else {  
  7. //$(this).html('隐藏');  
  8. $(this).css({"background":"url(/images/btn_arrow_up.png) no-repeat"});  
  9. }  
  10. $(this).next().slideToggle('fast');  
  11. }); 

方法二:

 

 
  1. $('#para_div button').click(function() {  
  2. if($(this).next().css('display') == 'none') {  
  3. //$(this).html('隐藏');  
  4.  $(this).css({"background":"url(/images/btn_arrow_up.png) no-repeat"}); 
  5. }  
  6. else{  
  7. //$(this).html('显示');  
  8.  $(this).css({"background":"url(/images/btn_arrow_down.png) no-repeat"});  
  9. }  
  10. $(this).next().slideToggle('fast');  
  11. }); 

方法三:

 

 
  1. $('#para_div button').click(function() {  
  2. var $cn = $(this).next();  
  3. //$(this).html(($cn.is(":visible")) ? '显示' : '隐藏');  
  4. (this).css(($cn.is(":visible")) ?  
  5. {"background":"url(images/btn_arrow_down.png) no-repeat"} :  
  6. {"background":"url(images/btn_arrow_up.png) no-repeat"});  
  7. $cn.toggle('fast'); 
  8. }); 

希望本文所述对大家的jQuery程序设计有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选