2017-07-04 59 views
0

嗨,我想切换两个div。我做了基本的方法,但我真的想这样写,我不知道这有什么问题。有人可以帮我吗?切换两个div查询

(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()像下面

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

我在VAR代替这一点,仍然没有对我来说不是作品 –

0

切换DIV简单地使用jQuery。您可以点击这里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'); 
} 
}); 

试试这个它应该工作,请了,如果工作