這篇文章主要介紹了jQuery判斷一個元素是否可見的方法,涉及jQuery鏈?zhǔn)讲僮骷皹邮脚卸ǖ募记?需要的朋友可以參考下
本文實例講述了jQuery判斷一個元素是否可見的方法。分享給大家供大家參考。具體如下:
jQuery 可以很容易地確定一個元素是可見的或是隱藏的,然后分別做不同的處理。如:我想根據(jù)某 div 是否可見,在按鈕上顯示不同的文字和圖標(biāo)。可以這樣實現(xiàn):
方法一:
- $('#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")) ? '顯示' : '隱藏');
- (this).css(($cn.is(":visible")) ?
- {"background":"url(images/btn_arrow_down.png) no-repeat"} :
- {"background":"url(images/btn_arrow_up.png) no-repeat"});
- $cn.toggle('fast');
- });
希望本文所述對大家的jQuery程序設(shè)計有所幫助。
新聞熱點
疑難解答
圖片精選