Javascript 切换两个div查询
嗨,我想切换两个div。我用基本的方法写,但我真的想用这种方法写,我不知道这有什么不对。有人能帮我吗Javascript 切换两个div查询,javascript,jquery,toggle,hide,show,Javascript,Jquery,Toggle,Hide,Show,嗨,我想切换两个div。我用基本的方法写,但我真的想用这种方法写,我不知道这有什么不对。有人能帮我吗 (function($) { var showAll = function() { this.$button = this.find('.btn-all-events'); this.$all = this.find('.all-events'); this.$recent = this.find('.recent-events'
(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);
使用$(this.find()
而不是像下面这样使用this.find()
$(this).find('.btn-all-events');
使用jquery简单地切换div。你可以在这里查一下
html--
Divdfasdfasdfsd
AFSDFASDFASDFSF
切换
css--
.隐藏{
显示:无;
}
js---
$('.toggle')。单击(函数(){
if($('.first').hasClass('hide')){
$('.first').removeClass('hide');
}否则{
$('.first').addClass('hide');
}
if($('.second').hasClass('hide')){
$('.second').removeClass('hide');
}否则{
$('.second').addClass('hide');
}
});
试试这个,它应该可以工作,如果可以工作,请升级我在var中替换了这个,但仍然不适合我
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');
}
});