网络编程 
首页 > 网络编程 > 浏览文章

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

(编辑:jimmy 日期: 2025/7/3 浏览:3 次 )

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

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

方法一:

$('#para_div button').click(function() {  
   if($(this).next().is(":visible")) {        
    //$(this).html('显示');  
    $(this).css({"background":"url(/images/btn_arrow_down.png) no-repeat"});  
   }  
   else {  
    //$(this).html('隐藏');   
    $(this).css({"background":"url(/images/btn_arrow_up.png) no-repeat"});    
   }   
  $(this).next().slideToggle('fast');  
 });

方法二:

$('#para_div button').click(function() {  
   if($(this).next().css('display') == 'none') {        
    //$(this).html('隐藏');   
 $(this).css({"background":"url(/images/btn_arrow_up.png) no-repeat"});
   }  
   else{  
    //$(this).html('显示');  
 $(this).css({"background":"url(/images/btn_arrow_down.png) no-repeat"});   
   }   
  $(this).next().slideToggle('fast');  
});

方法三:

$('#para_div button').click(function() {  
 var $cn = $(this).next();  
 //$(this).html(($cn.is(":visible")) ":visible")) "background":"url(images/btn_arrow_down.png) no-repeat"} :  
{"background":"url(images/btn_arrow_up.png) no-repeat"});  
 $cn.toggle('fast');
});

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

上一篇:jQuery实现延迟跳转的方法
下一篇:jquery超简单实现手风琴效果的方法
一句话新闻
高通与谷歌联手!首款骁龙PC优化Chrome浏览器发布
高通和谷歌日前宣布,推出首次面向搭载骁龙的Windows PC的优化版Chrome浏览器。
在对骁龙X Elite参考设计的初步测试中,全新的Chrome浏览器在Speedometer 2.1基准测试中实现了显著的性能提升。
预计在2024年年中之前,搭载骁龙X Elite计算平台的PC将面世。该浏览器的提前问世,有助于骁龙PC问世就获得满血表现。
谷歌高级副总裁Hiroshi Lockheimer表示,此次与高通的合作将有助于确保Chrome用户在当前ARM兼容的PC上获得最佳的浏览体验。