2017-07-04 3 views
0

こんにちは、私は2つのdivを切り替えるしたいと思います。私はそれを基本的な方法で作ったが、私は本当にこのように書きたいと思うし、何が間違っているのか分からない。誰かが私にそれを手伝ってもらえますか?2つのdivsクエリを切り替える

(function($) { 

    var showAll = function() { 
     this.$button = this.find('.btn-all-events'); 
     this.$all = this.find('.all-events'); 
     this.$recent = this.find('.recent-events'); 

     this.initEvents(); 
    }; 

    showAll.prototype = { 
     initEvents: function() { 

      if (this.$all) { 
       this.$button.on('click', this.showEvents.bind(this)); 
      } 
     }, 

     showEvents: function() { 
      this.$recent.hide(); 
      this.$all.show(); 
     } 
    }; 

    this.toggle = new showAll($(this)); 
})(jQuery); 

答えて

0

使用$(this).find()ないthis.find()トグルは、単にjqueryのを使用してDIV

$(this).find('.btn-all-events'); 
+0

を働いた場合、最大喜ば、それが動作するはずこれを試して、私はVARでこれを交換し、まだ私のためではない作品をして確認することができます –

0

以下のように。あなたはここにhttps://jsfiddle.net/surendra786/os09Lhfw/

html -- 
    <div class="main"> 
    <div class="first">Divdfasdfasdfsd</div> 
    <div class="second hide">afsdfasfasdfasdfsdf</div> 
    </div> 
    <a class="toggle">toggle</a> 

css-- 

<style> 
.hide { 
display:none; 
} 
</style> 

js--- 

$('.toggle').click(function(){ 
if($('.first').hasClass('hide')){ 
$('.first').removeClass('hide'); 
} else { 
$('.first').addClass('hide'); 
} 
if($('.second').hasClass('hide')){ 
$('.second').removeClass('hide'); 
} else { 
$('.second').addClass('hide'); 
} 
}); 

関連する問題