Javascript 在网站上用jquery弹出页脚,第一次点击?
我对jquery弹出的页脚有疑问。我的代码是有效的,但唯一的问题是,第一次点击按钮时,它不起作用,第二次点击按钮时,它就起作用了?有什么想法吗? 这是我的密码:Javascript 在网站上用jquery弹出页脚,第一次点击?,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我对jquery弹出的页脚有疑问。我的代码是有效的,但唯一的问题是,第一次点击按钮时,它不起作用,第二次点击按钮时,它就起作用了?有什么想法吗? 这是我的密码: <script type="text/javascript"> jQuery(function($) { var open = false; $('#footerSlideButton').click(function () { if(open ===
<script type="text/javascript">
jQuery(function($) {
var open = false;
$('#footerSlideButton').click(function () {
if(open === false) {
$('#footerSlideContainer').animate({ height: '0' });
$(this).css('backgroundPosition', 'top left');
open = true;
} else {
$('#footerSlideContainer').animate({ height: '150px' });
$(this).css('backgroundPosition', 'bottom left');
open = false;
}
});
});
</script>
jQuery(函数($){
var open=false;
$(“#页脚滑动按钮”)。单击(函数(){
如果(打开===错误){
$('#footerSlideContainer')。设置动画({height:'0'});
$(this.css('backgroundPosition','top left');
开放=真实;
}否则{
$('#footerSlideContainer')。设置动画({height:'150px'});
$(this.css('backgroundPosition','bottomleft');
开=假;
}
});
});
我将其浓缩为:
(function($){
window.opened=false;
$('#footerSlideButton').on('click', function () {
$('#footerSlideContainer').animate({ height : opened ? '0' : '150px'});
$(this).css('backgroundPosition', opened ? 'top left' : 'bottom left');
opened = !opened;
});
})(jQuery);
您还可以将打开状态存储在元素数据中:
(function($){
$('#footerSlideButton').on('click', function () {
$('#footerSlideContainer').animate({ height : $(this).data('open') ? '0' : '150px'});
$(this).css('backgroundPosition', $(this).data('open') ? 'top left' : 'bottom left');
$(this).data('open', !$(this).data('open'));
});
})(jQuery);
应
var open=true代码>bevar open=false代码>?旁注:对于if
语句,如果var是bool,您可以简单地说if(open)
表示true,if(!open)
表示false oops。对不起,如果是false,我也有同样的问题。它通过第二次点击按钮来工作。我想在第一次点击时弹出页脚。你认为它与css有关吗?为什么要使用窗口。在函数内部打开?我不这么认为。它应该是var open=false代码>附言。我同意你的看法。windows.open不起作用。以上两个代码都不起作用!!是的,window.open是我的一个大疏忽。即使我改变了窗口。open页脚不会弹出,但是我的代码会弹出,但是第二次点击!!!您需要提供您的代码,因为它在fiddler上工作: