如何使用JavaScript代码在if条件下创建函数方法?
我想在JavaScript代码中给出condition方法。此代码在Internet Explorer 10版本中不起作用。粘贴此代码时,将显示以下消息: 函数声明不应放在块中。使用函数表达式或将语句移动到外部函数的顶部 这里怎么能有函数如何使用JavaScript代码在if条件下创建函数方法?,javascript,html,function,Javascript,Html,Function,我想在JavaScript代码中给出condition方法。此代码在Internet Explorer 10版本中不起作用。粘贴此代码时,将显示以下消息: 函数声明不应放在块中。使用函数表达式或将语句移动到外部函数的顶部 这里怎么能有函数 if(jQuery("header").attr("id") == "header-style-two") { function sticky_relocate() {
if(jQuery("header").attr("id") == "header-style-two")
{
function sticky_relocate() {
var window_top = jQuery(window).scrollTop();
var div_top = jQuery('#sticky-anchor').offset().top;
if (window_top > div_top) {
jQuery('.mainmenu-area').removeClass('light-menu');
//This is for when div in top
} else {
jQuery('.mainmenu-area').addClass('light-menu');
//This is for when div in not top
}
}
jQuery(function () {
jQuery(window).scroll(sticky_relocate);
sticky_relocate();
});
}
(根据规范,它是无效的,这会使它变得相当混乱,但一些JavaScript引擎可能会试图补偿错误代码,这就是为什么它可能在某些浏览器中工作的原因),但您可以将函数分配给if块中的变量
而不是:
function sticky_relocate() {
您可以这样做:
var sticky_relocate = function() {
所以像这样的东西会起作用:
if(jQuery("header").attr("id") == "header-style-two")
{
var sticky_relocate = function() {
var window_top = jQuery(window).scrollTop();
var div_top = jQuery('#sticky-anchor').offset().top;
if (window_top > div_top) {
jQuery('.mainmenu-area').removeClass('light-menu');
//This is for when div in top
} else {
jQuery('.mainmenu-area').addClass('light-menu');
//This is for when div in not top
}
}
jQuery(function () {
jQuery(window).scroll(sticky_relocate);
sticky_relocate();
});
}
JSHint给你一个警告,意思是“我们认为这不是最好的主意,但我们会允许你这么做。” 您可以通过几种方式更改代码 将函数声明更改为函数表达式:
if(jQuery("header").attr("id") == "header-style-two") {
var sticky_relocate = function() {
var window_top = jQuery(window).scrollTop();
var div_top = jQuery('#sticky-anchor').offset().top;
if (window_top > div_top) {
jQuery('.mainmenu-area').removeClass('light-menu');
//This is for when div in top
} else {
jQuery('.mainmenu-area').addClass('light-menu');
//This is for when div in not top
}
}
jQuery(function () {
jQuery(window).scroll(sticky_relocate);
sticky_relocate();
});
}
或者将函数定义移出if
块
if(jQuery("header").attr("id") == "header-style-two") {
jQuery(function () {
jQuery(window).scroll(sticky_relocate);
sticky_relocate();
});
}
function sticky_relocate() {
var window_top = jQuery(window).scrollTop();
var div_top = jQuery('#sticky-anchor').offset().top;
if (window_top > div_top) {
jQuery('.mainmenu-area').removeClass('light-menu');
//This is for when div in top
} else {
jQuery('.mainmenu-area').addClass('light-menu');
//This is for when div in not top
}
}
在if条件下不能有函数声明。你可以把它放在外面,根据情况给它打电话
var isHeaderStyleTwo = (jQuery("header").attr("id") == "header-style-two");
function sticky_relocate() {
var window_top = jQuery(window).scrollTop();
var div_top = jQuery('#sticky-anchor').offset().top;
if (window_top > div_top) {
jQuery('.mainmenu-area').removeClass('light-menu');
//This is for when div in top
} else {
jQuery('.mainmenu-area').addClass('light-menu');
//This is for when div in not top
}
}
jQuery(function () {
if (isHeaderStyleTwo) {
jQuery(window).scroll(sticky_relocate);
sticky_relocate();
}
首先,为什么有人需要把函数放在if块中?为什么我们不能把它命名function@A.T.您可能希望有条件地将函数设置为变量,或者避免创建不想使用的函数。非常感谢35; Alexander。这段代码起作用了,我的问题也解决了:)在你的回答中值得一提的是,为什么我们不能在if blockThank@Mahi中命名函数。这对我很有帮助。非常感谢:)