当我编写脚本时,它显示引用错误:未定义宽度
When i write the script it shows ReferenceError: theWidth is not defined
这是我的代码:
$(window).load(function() {
var theImage = $('ul li img');
var theWidth = theImage.width();
//wrap into mother div
$('ul').wrap('<div class="gallery-slide" />');
//assign height width and overflow hidden to gallery-slide
$('.gallery-slide').css({
width: function() {
return theWidth;
},
height: function() {
return theImage.height();
},
position: 'relative',
overflow: 'hidden'
});
//get total of image sizes and set as width for ul
var totalWidth = theImage.length * theWidth;
$('ul').css({
width: function() {
return totalWidth;
}
});
});
$('ul li img').each(function(intIndex) {
$(this).nextAll('a').bind("click", function() {
if ($(this).is(".next")) {
$(this).parent('li').parent('ul').animate({
"margin-left": (-(intIndex + 1) * theWidth)
}, 1000);
} else if ($(this).is(".previous")) {
$(this).parent('li').parent('ul').animate({
"margin-left": (-(intIndex - 1) * theWidth)
}, 1000);
} else if ($(this).is(".startover")) {
$(this).parent('li').parent('ul').animate({
"margin-left": (0)
}, 1000);
}
}); //close .bind()
}); //close .each()
以上是我的代码,它抛出错误theWidth
未定义。
JavaScript 具有
function-level
范围(在ECMAScript 6
之前),如果在curly braces
包围的某个代码块中声明的变量仅在该代码块中可见,并且该变量在该特定代码块之外不可见。
theWidth
是在$(window).load
的作用域下定义的,该作用域undefined
超出.load
处理程序的作用域。
代码包装在load
处理程序中。
$(window).load(function() {
var theImage = $('ul li img');
var theWidth = theImage.width();
//wrap into mother div
$('ul').wrap('<div class="gallery-slide" />');
//assign height width and overflow hidden to gallery-slide
$('.gallery-slide').css({
width: function() {
return theWidth;
},
height: function() {
return theImage.height();
},
position: 'relative',
overflow: 'hidden'
});
//get total of image sizes and set as width for ul
var totalWidth = theImage.length * theWidth;
$('ul').css({
width: function() {
return totalWidth;
}
});
$('ul li img').each(function(intIndex) {
$(this).nextAll('a').bind("click", function() {
if ($(this).is(".next")) {
$(this).parent('li').parent('ul').animate({
"margin-left": (-(intIndex + 1) * theWidth)
}, 1000);
} else if ($(this).is(".previous")) {
$(this).parent('li').parent('ul').animate({
"margin-left": (-(intIndex - 1) * theWidth)
}, 1000);
} else if ($(this).is(".startover")) {
$(this).parent('li').parent('ul').animate({
"margin-left": (0)
}, 1000);
}
});
});
});
这是因为你在一个函数中声明theWidth
,然后尝试在该函数范围之外的单独函数中使用它。
这就是 scope 在 javascript 中的工作方式:
var foo = 'bar';
function f() {
console.log(foo); // works!
}
这是有效的,因为函数f
在声明foo
的作用域内......所以你可以使用它。
function e() {
var foo = 'bar';
}
function f() {
console.log(foo); // doesn't work!
}
var foo
是在 e
范围内声明的,而 f
不在 e
的范围内,所以 foo 对它不可用。
function e() {
var foo = 'bar';
function f() {
console.log(foo); // works again!
}
}
现在它再次工作,因为f
在声明foo
的e
范围内。
希望这将有助于展示范围的工作原理。
相关文章:
- gmaps4rails 2.4.6未捕获引用错误:未定义Gmaps
- Wordpress中的Javascript出现匿名函数/引用错误
- 使用dc.js、d3.js和crossfilter引用错误
- 未捕获引用错误:未定义validateform
- 为什么我会出现此错误"未捕获引用错误:未定义标题;
- $未定义-未捕获引用错误
- Sitecore”;未捕获引用错误:未定义MultilistWithSearch;
- Wordpress:未捕获引用错误:未定义速度
- Node.js引用错误:未定义类名
- 我该如何解决“;未捕获引用错误:谷歌没有定义"?(谷歌地图API)
- Javascript 未定义错误:引用错误:未定义 ALT
- 为流星重新包装砌体会产生错误“引用错误:窗口未定义”
- 未定义错误引用
- 错误: 引用错误: 未定义发送
- 角度砌体指令隐藏内部元素,错误:引用错误:未定义砌体
- CoffeeScript 在 'for v in values' 中创建一个全局变量 'v',导致事件中的错误引用
- Javascript:未捕获类型错误:引用创建的对象时无法调用未定义的方法“add”
- 架构错误 - 引用错误:未定义电话
- 获取错误“引用错误:未定义事件”
- 打字稿错误引用_this