错误 var 未定义 - 但在使用之前已定义 -

发布于 2024-12-09 10:19:35 字数 1438 浏览 1 评论 0原文

此代码

    $(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

撞了怀 2024-12-16 10:19:35

您已在“就绪”处理函数内部定义了变量。这意味着它们对于该函数是私有的,并且在该函数之外不可见。

您可以通过显式设置它们的 window 属性来使它们可见:

  window['hBack'] = whatever;

或者您也可以将“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:

  window['hBack'] = whatever;

or you could put your "closeCurrent" function inside the "ready" handler too, so long as it's only referenced by code within the handler.

辞慾 2024-12-16 10:19:35

一言以蔽之,范围。在 $(document).ready 函数外部声明变量以使它们进入全局范围,然后在函数内部设置它们。

var wBack, hBack, lback, tback;
$(document).ready(function () {
    wBack = ...

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.

var wBack, hBack, lback, tback;
$(document).ready(function () {
    wBack = ...
策马西风 2024-12-16 10:19:35

这是一个范围问题。

这里的其他答案在技术上是可行的,但如果您不需要全局变量,我绝不会建议将它们保留为全局变量。

您正在 DOM-ready 块内定义 wBackhBacklBacktBack,但您正在尝试从事件处理程序访问它们。如果可以的话,从元素中删除 onclick ,并将其添加到 DOM-ready 块中以共享变量。

$(document).ready(function(){

    // Store the selector
    var element = $('ul li.roundabout-in-focus')
    var wBack = element.height();
    var hBack = element.width();
    var lback = element.css('left');
    var tback = element.css('top');

   $("#someElement").click(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();
   })
});

您还应该将选择器缓存在该块的顶部(如我的示例中),以避免重复查询 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, and tBack inside the DOM-ready block, but you are trying to access them from the event handler. if you can, remove the onclick from the element, and add it inside the DOM-ready block to share the variables.

$(document).ready(function(){

    // Store the selector
    var element = $('ul li.roundabout-in-focus')
    var wBack = element.height();
    var hBack = element.width();
    var lback = element.css('left');
    var tback = element.css('top');

   $("#someElement").click(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();
   })
});

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.

灯角 2024-12-16 10:19:35

该变量仅在您传递给 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 the var keyword)

那片花海 2024-12-16 10:19:35

您的变量(wBack、hBack、lback 和 tback)刚刚在 document.ready 函数中定义。没有全球性的。

为了能够在其他函数中使用它,您的变量必须是全局的。
您可以删除变量之前的“var”,因为在 javascript 中,没有“var”的变量声明会使该变量成为全局变量。或者您可以像这样手动将其声明为全局:

var wBack;      
var hBack;
var lback;
var tback;
$(document).ready(function(){
   wBack = $('ul li.roundabout-in-focus').height();
   hBack = $('ul li.roundabout-in-focus').width();
   lback = $('ul li.roundabout-in-focus').css('left');
   tback = $('ul li.roundabout-in-focus').css('top');
});

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 :

var wBack;      
var hBack;
var lback;
var tback;
$(document).ready(function(){
   wBack = $('ul li.roundabout-in-focus').height();
   hBack = $('ul li.roundabout-in-focus').width();
   lback = $('ul li.roundabout-in-focus').css('left');
   tback = $('ul li.roundabout-in-focus').css('top');
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文