jJavascript window.onload事件正确用法

发布于 2024-12-03 06:49:19 字数 559 浏览 0 评论 0原文

我有这个循环代码来减少 Javascript 中的 DOM 调用,并重用它们。

aarr = [];

for (var z=1; z<=10; z++) {
    c = z-1;
    aarr[c] = document.getElementById("a"+z); 
}

我已经证明,如果代码在 DOM 完成之前运行,则数组为空。将脚本移到最后一个 html 代码之后即可。

所以现在我想将此代码放入 window.onload 事件中,这样就不必将脚本代码移动到页面底部。但它显然不起作用,因为看起来数组循环是在 DOM 完成之前执行的。

window.onload=function(){

    var aarr = [];
    for (var z=1; z<=10; z++) {
        c = z-1;
        aarr[c] = document.getElementById("a"+z);
    }
}

另外,我尝试删除“var”以删除范围而不产生任何影响。

I have this loop code to reduce the DOM calls in my Javascript, and reuse them.

aarr = [];

for (var z=1; z<=10; z++) {
    c = z-1;
    aarr[c] = document.getElementById("a"+z); 
}

I have been shown that if the code is ran before the DOM is complete, then the array is null. Moving the script after the last html code will work.

So now I want to put this code inside the window.onload event so to not have to move the script code to the bottom of the page. But it apparently does not work because it appears that the array loop is executed before the DOM is completed.

window.onload=function(){

    var aarr = [];
    for (var z=1; z<=10; z++) {
        c = z-1;
        aarr[c] = document.getElementById("a"+z);
    }
}

Also, I have tried to remove the "var" to remove scope without making a difference.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

淡莣 2024-12-10 06:49:19

您也可以在不使用框架的情况下尝试这种方法:

window.onload = (function(){
    return function(){
        var aarr = [];
        for (var z=1; z<=10; z++) {
            aarr.push(document.getElementById("a"+z));
               alert(aarr[z-1].id);
        }
     };
})();

JSFiddle

You could also try this approach without using a framework:

window.onload = (function(){
    return function(){
        var aarr = [];
        for (var z=1; z<=10; z++) {
            aarr.push(document.getElementById("a"+z));
               alert(aarr[z-1].id);
        }
     };
})();

JSFiddle

我的影子我的梦 2024-12-10 06:49:19

如果您可以使用 jquery,那么您可以使用文档就绪侦听器:

$(document).ready(function() {

  var aarr = [];
  for (var z=1; z<=10; z++) {
    c = z-1;
    aarr[c] = document.getElementById("a"+z);

 }

});

http://www.jquery.com

作为根据上面的评论,您是否尝试过:

if (document.readyState === "complete") { init(); } // call whatever function u want.

If you can use jquery, then you can use the document ready listener:

$(document).ready(function() {

  var aarr = [];
  for (var z=1; z<=10; z++) {
    c = z-1;
    aarr[c] = document.getElementById("a"+z);

 }

});

http://www.jquery.com

as per the comment above, have you tried:

if (document.readyState === "complete") { init(); } // call whatever function u want.
寄居人 2024-12-10 06:49:19

加载事件在文档加载过程结束时触发。至此,文档中的所有对象都已在 DOM 中,所有图像和子框架也已加载完毕。

MDN - window.onload

我猜您尝试在 onload 之外调用代码。 查看这个小提琴

The load event fires at the end of the document loading process. At this point, all of the objects in the document are in the DOM, and all the images and sub-frames have finished loading.

MDN - window.onload

I guess you try calling code outside of onload. See this fiddle

离旧人 2024-12-10 06:49:19

最好使用不预扫描 dom 的函数来创建缓存,当您使用带有缓存构造的简单函数时,不需要预扫描。使用 jQuery,您可以创建这样的函数(下面是本机 javascript 方法):

window.__jcache = {};

  window.$jc = function(u) // jQuery cache
  {
   if( u == undefined )
    { return window.jQuery; }
   if( typeof u == 'object' || typeof u == 'function' )
    { return window.jQuery(u); }
   if( window.__jcache[u] == undefined )
    { window.__jcache[u] = window.jQuery(u); }

   return window.__jcache[u]; 
  };

或者没有框架(本机 javascript):

window.__domcache = {};

  window.getObj = function(u) // jQuery cache
  {
   if( u == undefined )
    { return null; }
   if( typeof u == 'object' || typeof u == 'function' )
    { return u; }
   if( window.__domcache[u] == undefined )
    { window.__domcache[u] = document.getElementById(u); }

   return window.__domcache[u]; 
  };

所以您可以这样做:

var o = $jc('a1'); // for jquery version

var o = getObj('a1'); // The native javascript method (jamex)

就可以了。

欧文·汉杰斯·格雷茨

Better to use a function without pre-scanning the dom to create a cache, Pre-scanning is not needed when you use a simple function with a cache construction. With jQuery you can can create a function like this (native javascript method below this):

window.__jcache = {};

  window.$jc = function(u) // jQuery cache
  {
   if( u == undefined )
    { return window.jQuery; }
   if( typeof u == 'object' || typeof u == 'function' )
    { return window.jQuery(u); }
   if( window.__jcache[u] == undefined )
    { window.__jcache[u] = window.jQuery(u); }

   return window.__jcache[u]; 
  };

Or without a framework (native javascript):

window.__domcache = {};

  window.getObj = function(u) // jQuery cache
  {
   if( u == undefined )
    { return null; }
   if( typeof u == 'object' || typeof u == 'function' )
    { return u; }
   if( window.__domcache[u] == undefined )
    { window.__domcache[u] = document.getElementById(u); }

   return window.__domcache[u]; 
  };

So you can do:

var o = $jc('a1'); // for jquery version

var o = getObj('a1'); // The native javascript method (jamex)

That does the trick.

Greetz, Erwin Haantjes

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文