2013-11-22 192 views
0

hai im使用顯示/隱藏滑塊與div..evrything工作正常與我的代碼.. 但我想我的div或我的內容會自動隱藏,當我點擊另一個按鈕(在我的編碼圖像)..如何使div自動隱藏當點擊其他按鈕

我希望有人能幫助我提前this..thanks ..

這是我的HTML

<div id="slidingDiv"> 
    <a href="#" class="show_hide" rel="#slidingDiv11"> 
     <img src="../img/choose/mens.png"> 
    </a> 

    <a href="#" class="show_hide" rel="#slidingDiv12"> 
     <img src="../img/choose/womens.png"> 
    </a> 
    <div id="slidingDiv11"> 
     <img src="../img/choose/clothings.png"><br><br> 
     <img src="../img/choose/shoes.png"><br><br> 
     <img src="../img/choose/bags.png"><br><br> 
     <img src="../img/choose/accessories.png"> 
    </div> 
    <div id="slidingDiv12"> 
     <img src="../img/choose/clothings.png"><br><br> 
     <img src="../img/choose/shoes.png"><br><br> 
     <img src="../img/choose/bags.png"><br><br> 
     <img src="../img/choose/accessories.png"> 
    </div> 
</div> 
<div id="slidingDiv1"> 
    <a href="#" class="show_hide" rel="#slidingDiv16"> 
     <img src="../img/choose/book.png"> 
    <a> 
    <a href="#" class="show_hide" rel="#slidingDiv17"> 
     <img src="../img/choose/textbooks.png"> 
    </a> 

    <div id="slidingDiv16"> 
     <img src="../img/choose/books1.png"> 
     <img src="../img/choose/books1.png"> 
     <img src="../img/choose/books1.png"> 
    </div> 
    <div id="slidingDiv17"> 
     <img src="../img/choose/textbooks1.png"> 
     <img src="../img/choose/textbooks1.png"> 
     <img src="../img/choose/textbooks1.png"> 
    </div> 
</div> 

這是我的CSS

#slidingDiv, #slidingDiv1, #slidingDiv2, #slidingDiv3, #slidingDiv4, #slidingDiv5, #slidingDiv6, #slidingDiv7, #slidingDiv8, #slidingDiv9, #slidingDiv10, #slidingDiv11, #slidingDiv12, #slidingDiv13, #slidingDiv14, #slidingDiv15, #slidingDiv16, #slidingDiv17 { 
    height:300px; 
    padding:20px; 
    margin-top:10px; 
    border-bottom:5px; 
    display:none; 
} 

這是我的js

$(document).ready(function() { 
    $('.show_hide').showHide({ 
     speed: 1000, // speed you want the toggle to happen 
     easing: '', // the animation effect you want. Remove this line if you dont want an effect and if you haven't included jQuery UI 
     changeText: 0, // if you dont want the button text to change, set this to 0 
     showText: 'View', // the button text to show when a div is closed 
     hideText: 'Close' // the button text to show when a div is open 
    }); 
}); 

(function ($) { 
    $.fn.showHide = function (options) { 

     //default vars for the plugin 
     var defaults = { 
      speed: 1000, 
      easing: '', 
      changeText: 0, 
      showText: 'Show', 
      hideText: 'Hide' 

     }; 
     var options = $.extend(defaults, options); 

     $(this).click(function() { 

      $('.toggleDiv').slideUp(options.speed, options.easing); 
      // this var stores which button you've clicked 
      var toggleClick = $(this); 
      // this reads the rel attribute of the button to determine which div id to toggle 
      var toggleDiv = $(this).attr('rel'); 
      // here we toggle show/hide the correct div at the right speed and using which easing effect 
      $(toggleDiv).slideToggle(options.speed, options.easing, function() { 
       // this only fires once the animation is completed 
       if (options.changeText == 1) { 
        $(toggleDiv).is(":visible") ? toggleClick.text(options.hideText) : toggleClick.text(options.showText); 
       } 
      }); 

      return false; 
     }); 
    }; 
})(jQuery); 
+2

請注意正確格式化您的代碼。它使你和其他人更容易閱讀。 –

+1

你可以讓小提琴它會有幫助。 –

+1

jsfiddle.net小提琴吧 –

回答

0

既然你要隱藏的div當你點擊一個按鈕,你應該使它成爲document點擊隱藏。嘗試這樣的事情。

$(".hide_show").click(function(event) { event.stopPropagation(); $(this).fadeToggle(1000);}); $(document).click(function() { $(".hide_show).fadeOut(1000); });

使用event.stopPropagation();停止文檔事件觸發.hide_show類。可能會幫助你。

+0

謝謝..催情,即時通訊新的編程..我應該把嗎? 以及如何使用event.stopPropagation(); – user

+0

對不起。好的,如果你有兩個帶'.click_toggle'和'toggle_div'類的'div',並且你想在點擊'click_toggle'時隱藏和顯示'.toggle_div'。你應該像'$(「。click_toggle」)一樣應用'jquery'。click(function(){$(「。toggle_div」)。toggle();});'現在你想隱藏'toggle_dive'當你點擊它之外'時,你應該應用另一個代碼,像這樣'$(document).click(function(){$(「。toggle_div」)。hide();});'如果你不想要文檔(函數(e)(e.stopPropagation(); – amostk

+0

)而不是'$(「。click_toggle」)。click(function (){$(「。toggle_div」)。toggle();});'因爲如果你沒有添加'e.stopPropagation();'可能會導致Js故障。 'e'。 – amostk

2

使用jQuery,

$("your_other_button").on('click', function() { 
     $("your_div_to_hide").hide(); 
}); 
+0

我在哪裏可以寫這個? – user

1

使用jQuery

$("other_button").on('click', function() { 
    $("div_to_hide").toggle(); 
}); 
+0

我應該在哪裏放? – user

+0

你可以添加此HTML體正文結束前腳本標記 – Jijo

相關問題