我找到类似的答案,解决了第一次调用jQuery函数时的双击问题,通常是在另一个click()内部有click()函数或者缺少$(document).ready。这些解决方案都没有为我解决这个问题。当页面首次加载时,我必须单击按钮一次,然后再次激活该功能。奇怪的是,这只是Firefox中的一个问题,它在Chrome中正常运行。
HTML:
<li class="nav-item">
<a id="resize-fix" class="nav-link full-button" alt="Full-Width Resize" title="Full-Width Resize"><i class="lnr lnr-frame-expand"></i></a>
</li>
JavaScript的:
$(document).ready(function(){
if ($.cookie('fullscreen') == "yes") {
$('#resize-fix').find('i').removeClass('lnr-frame-expand');
$('#resize-fix').find('i').addClass('lnr-frame-contract');
$('.container').addClass('box-full');
}
// When the icon is clicked
$("#resize-fix").click(function () {
// Check the current cookie value
// If the cookie is empty or set to no, then add fullscreen
if ($.cookie('fullscreen') == "undefined" || $.cookie('fullscreen') == "no") {
// Set cookie value to yes
$.cookie('fullscreen','yes', {expires: 7, path: '/'});
// Add the class to the container
$(this).find('i').toggleClass('lnr-frame-expand lnr-frame-contract');
$('.container').addClass('box-full');
}
// If the cookie was already set to yes then remove it
else {
$.cookie('fullscreen','no', {expires: 7, path: '/'});
// Remove the class from the container
$(this).find('i').toggleClass('lnr-frame-expand lnr-frame-contract');
$('.container').removeClass('box-full');
}
});
});