4

横に6つのナビゲーションボタンがあるサイトがあります。それらのそれぞれについて、対応する DIV を表示し、他の 5 を非表示にしたいと考えています。

hide()、show()、fadeなどで要素を非表示および表示する方法を知っていますが、スペルに頼らずに、現在表示されているものを同時に非表示にしながら、クリックされたものをシームレスに表示する最良の方法を考え出そうとしていますそれらはすべて次のようになります。

$('#btn1').click(function(){
$('#div2').hide();
$('#div3').hide();
$('#div1').show();
 )}
4

4 に答える 4

1
$('.commonBtn').click(function(){ // commonBtn is common class to all buttons
   var index = this.id.replace('btn','');
   $('div[id^=div]:visible').hide();
   $('#div'+ index).show();
)};
于 2012-05-22T17:19:20.680 に答える
1

jqueryコードは次のとおりです。

      $(document).ready(function() {
          $('#btn-next').click(function () {
          $('#recent_post').hide();
           $('#top_post').fadeIn(3000).show();
           return false;        
            }); 
        $('#btn-prev').click(function () {
        $('#top_post').hide();
        $('#recent_post').fadeIn(3000).show();
            return false;   
        }); 
        });

html は次のとおりです。

    <div id="top_post" class="post" style="z-index:1;">
       <!---Content goes here--->
     </div>
    <div class="post" id="recent_post" style="display:none;z-index:0;">
       <!---Content goes here--->
     </div>

http://kaidul.web44.net/の「記事」セクションを参照し てください

于 2012-05-22T17:34:57.210 に答える
1

btn_1の代わりにボタンの id を使用するbtn1

$('div[id^="btn"]').click(function(){
    var id = $(this).attr("id").split("_")[1]; // fetch the id's number part

    $('div[id^="div"]').hide(); // hide all divs with id starting with div

    $("#div"+id).show(); // show corresponding div     

 )}
于 2012-05-22T17:24:23.437 に答える
0

これが私がやったことです。ナビゲーションボタンの名前が「dc」の場合、非表示になって表示されるdivは「dcdiv」という名前になります。

 var which_id;

    $(document).ready(function() {
        $("#rightcontent>div:not(.default)").hide();  // hide all client divs but default one
        $("#clientnav li").click(onClick); // event handler for nav bar

        function doTransition(){
            var which_name = which_id.split("#")[1];
            $('#clientnav li[id="'+which_name+'"]').addClass("active");
            $("#rightcontent>div:visible").slideUp("fast", function(){
                var which_div = which_id+'div';
                $(which_div).slideDown("fast");
            });
        }

        function onClick(event){
            $('#clientnav li').removeClass("active"); // remove active class for all nav buttons
            which_id = "#" + $(this).attr("id"); // get the id of the nav button clicked on
            doTransition();
            event.preventDefault();
        };
于 2012-05-22T18:02:57.167 に答える