简体   繁体   English

将jQuery选择器缓存到JavaScript对象?

[英]Cache jQuery Selectors to a JavaScript Object?

Based on code similar to the below javaScript, I am wanting to be able to use panelDNS.cache.propertyName to cache several commonly used jQuery Selectors 基于类似于以下javaScript的代码,我希望能够使用panelDNS.cache.propertyName缓存几个常用的jQuery选择器

The problem is it seems I need to wrap it inside of $(document).ready(function() { ) for it to work. 问题是似乎我需要将其包装在$(document).ready(function() { )中才能起作用。

Im just learning JS so I am not sure how I can do this and keep my short panelDNS.cache to access these values? 我只是在学习JS,所以我不确定如何做到这一点并保留我的short panelDNS.cache来访问这些值?

To clarify my question. 为了澄清我的问题。 Since panelDNS.cache is not a Function that I can Call inside my init function, I need to figure out how to make sure the DOM is loaded before panelDNS.cache gets called or ran 由于panelDNS.cache不是一个Function ,我可以在我的里面调用init函数,我需要弄清楚如何确保DOM加载之前panelDNS.cache被调用或跑

var panelDNS = {

  unsavedChanges: false,

  init: function () {
    $(document).ready(function () {
      PanelDNS.events();
    });
  },


  cache: {
    dnsTitleId: $("#dnsTitle"),
    translation: {
      absolute: 0,
      relative: 0,
      sinceDirectionChange: 0,
      percentage: 0
    }
  },

  events: {

  }

}

"I am asking, how I can make sure panelDNS.cache.xxx only gets called after the DOM s loaded" “我在问,如何确保panelDNS.cache.xxx仅在加载DOM之后被调用”

You are responsible for that. 您对此负责。 Make sure that your application code only gets executed after the DOM is loaded by kicking off the app in the document ready function: 通过在文档就绪功能中启动应用程序,确保仅在加载DOM之后才执行应用程序代码:

$(function(){
    //Your kickoff code
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM