错误 var 未定义 - 但在使用之前已定义 -
此代码
$(document).ready(function(){
var wBack = $('ul li.roundabout-in-focus').height();
var hBack = $('ul li.roundabout-in-focus').width();
var lback = $('ul li.roundabout-in-focus').css('left');
var tback = $('ul li.roundabout-in-focus').css('top');
});
function close_current(){
$('.nInfoIE').show();
$('.roundabout-in-focus').find('.img').show();
$('.roundabout-in-focus').css({position:'absolute',height:hBack,width:wBack,left:lBack,top:tBack});
$('.roundabout-in-focus').find('.iframe').css({'visibility':'hidden'});
$('ul li').find('.iframe').addClass('esconder');
$('ul li iframe').each(function(){
var tempurl = '';
tempurl = $(this).attr('src');
$(this).attr('src',tempurl.replace('?autoplay=1', ''));
});
watching = false;
$('.nInfoIE').hide();
}
通过 firebug 提示错误,警告 hBack 未定义,正如您所看到的,它是在 document.ready 上定义的,并且该函数在单击元素时执行...
-edit-
甚至尝试添加 var wBack ,hBack, lback,tback = 0;
在 document.ready 之前并删除 document.ready 中的单个“var”
我缺少什么?
this code
$(document).ready(function(){
var wBack = $('ul li.roundabout-in-focus').height();
var hBack = $('ul li.roundabout-in-focus').width();
var lback = $('ul li.roundabout-in-focus').css('left');
var tback = $('ul li.roundabout-in-focus').css('top');
});
function close_current(){
$('.nInfoIE').show();
$('.roundabout-in-focus').find('.img').show();
$('.roundabout-in-focus').css({position:'absolute',height:hBack,width:wBack,left:lBack,top:tBack});
$('.roundabout-in-focus').find('.iframe').css({'visibility':'hidden'});
$('ul li').find('.iframe').addClass('esconder');
$('ul li iframe').each(function(){
var tempurl = '';
tempurl = $(this).attr('src');
$(this).attr('src',tempurl.replace('?autoplay=1', ''));
});
watching = false;
$('.nInfoIE').hide();
}
prompts an error by firebug alerting that hBack is not defined, as you can see is defined on document.ready and the function is executed onClick an element...
-edit-
even tried to add var wBack ,hBack,lback,tback = 0;
before document.ready and removing the single 'var' inside document.ready
what am i missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您已在“就绪”处理函数内部定义了变量。这意味着它们对于该函数是私有的,并且在该函数之外不可见。
您可以通过显式设置它们的
window
属性来使它们可见:或者您也可以将“closeCurrent”函数放入“ready”处理程序中,只要它仅由处理程序中的代码引用即可。
You've defined the variables inside the "ready" handler function. That means that they're private to that function and not visible outside it.
You could make them visible by explicitly making them
window
properties:or you could put your "closeCurrent" function inside the "ready" handler too, so long as it's only referenced by code within the handler.
一言以蔽之,范围。在
$(document).ready
函数外部声明变量以使它们进入全局范围,然后在函数内部设置它们。In one word, scope. Declare your variables outside the
$(document).ready
function to get them into the global scope, and then set them inside the function.这是一个范围问题。
这里的其他答案在技术上是可行的,但如果您不需要全局变量,我绝不会建议将它们保留为全局变量。
您正在 DOM-ready 块内定义
wBack
、hBack
、lBack
和tBack
,但您正在尝试从事件处理程序访问它们。如果可以的话,从元素中删除onclick
,并将其添加到 DOM-ready 块中以共享变量。您还应该将选择器缓存在该块的顶部(如我的示例中),以避免重复查询 DOM(您也可以在单击处理程序中执行此操作,但选择器不同,因此我单独保留该函数: )
希望有帮助!干杯。
this is a scope issue.
the other answers here will technically work, but i'd never advise to keep those vars global if you don't need them globally.
you are defining
wBack
,hBack
,lBack
, andtBack
inside the DOM-ready block, but you are trying to access them from the event handler. if you can, remove theonclick
from the element, and add it inside the DOM-ready block to share the variables.you should also cache the selector at the top of that block (as in my example) to keep from querying the DOM repeatedly (you could probably do this in the click handler as well, but the selectors are different so i left that function alone :)
hope that helps! cheers.
该变量仅在您传递给
ready
函数的函数范围内定义。如果要引用它,需要在全局范围内声明它。 (只需删除var
关键字)The variable it's only defined in the scope of the function you pass to the
ready
function. If you want to reference it, you need to declare it in the global scope. (Just remove thevar
keyword)您的变量(wBack、hBack、lback 和 tback)刚刚在 document.ready 函数中定义。没有全球性的。
为了能够在其他函数中使用它,您的变量必须是全局的。
您可以删除变量之前的“var”,因为在 javascript 中,没有“var”的变量声明会使该变量成为全局变量。或者您可以像这样手动将其声明为全局:
Your variables (wBack ,hBack,lback and tback) are just defined in your document.ready function. There are not global.
To be able to use it in your other function, your variable must be global.
You can just remove the 'var' before your variable, because in javascript a variable declaration without the 'var' make the variable global. Or you can manually declare it global like this :