3

我无法访问对象中的某些元素。在我的视图文件(又名 HTML 页面)中,我正在初始化 c 应用程序。

  //Yh script tags are wrapped around this
  $(function() {
      App.initialize();
  });

然后在我的 JS 文件中(这是我实际工作的一种简单形式):

window.App = {
    el: {
        slider: $("#form-slider"),
        allSlides: $(".slide")

    },
    initialize: function() {
        this.ProductNewEvents();
            //bla bla
            // and so on
    },
    slideTiming: 800,
    slideWidth: 790,
    delegat etc
    ProductNewEvents: function() {
      //A whole bunch of events
       this.el.slider.click(function() {
           alert("Why you no work");
       });
       $("#form-slider").click(function() {
           alert("Why you work");
       });

    },
    some other objs
};

我的问题是我不能调用 this.el.allSlides.some jQuery 事件或this.el.slider. 假设在animate任何click对象中。我必须从 DOM 中获取它以将任何事件绑定到元素,例如$(".slide").animate.

4

1 回答 1

0

这取决于您在做什么,因为我看不到您的所有代码。当您访问您的属性时,它会在创建时App.el.slider保存一个引用。$('#slider')要克服这个问题,您的代码可能必须看起来像:

window.App = {
  el: {
    slider: function(){
      $("#form-slider");
    },
    allSlides: function(){
      $(".slide");
    }
  },
  initialize: function() {
    this.ProductNewEvents();
    //bla bla
    // and so on
  },
  slideTiming: 800,
  slideWidth: 790,
  //delegat etc
  ProductNewEvents: function() {
    //A whole bunch of events

  },
  //some other objs
}
App.el.slider();
于 2013-06-25T22:36:15.793 回答