【问题标题】:Caching DOM elements javascript缓存 DOM 元素 javascript
【发布时间】:2017-07-12 07:25:09
【问题描述】:

我正在尝试使用以下代码缓存页面初始化上的所有 DOM 元素:

init:function(){
      this.cacheDom();      
    }

cacheDom:function(){
       var that = this;
       this.$jsTabs              = this.container.find('.js-tabs');
       this.$li                  = $jsTabs.find('.tabs').find('li');
       this.$filterBodyTr   =  that.container.find('#filter_body').find("tr");
       this.$customRuleInfo = that.container.find('#custom_rule_info');

    },

enableTabs :function() {

     this.$li.click(function(){
      var index  = this.$li.index(this);
      this.$jsTabs.tabs().select(index);
      if(index==2){
        ****var tr = this.$filterBodyTr;****
        tr.each(function(){
            $t = $(this);
            if($t.is(":visible")){
                if ($t.text().toUpperCase().indexOf("MY NAME") > -1){
                    ****this.$customRuleInfo.addClass("hide-data");****
                }
            }
        });
      }

  });
},

这里 this.$filterBodyTr 和 this.$customRuleInfo 在 enableTabs 函数中标有 **** 的行中仍未定义。我尝试使用this 而不是that 来查找元素,但它总是显示未定义。

这里是引用元素的html代码

<table class="table-compressed" id = "js_filter_sort">
        <colgroup>
             <col class="column-lg-3">
             <col class="column-lg-1">
             <col class="column-lg-4">
             <col class="column-lg-2">
             <col class="column-lg-2">
        </colgroup>
        <tbody id = "filter_body"> </tbody>
 </table>



<div class= "row gutter-0 clear text-left padding-left-20 padding-right-20 padding-bottom-20 padding-top-20 hide-data" id = "custom_rule_info">                                               
                                        </div>

如何缓存标有 **** 的这 2 个元素。如果我不缓存元素并在 enableTabs 函数中初始化var that=this;,它就可以正常工作。

【问题讨论】:

  • this 回调内部的click 与外部不同。您需要使用 that 缓存它并更改回调中的引用。

标签: javascript jquery html caching


【解决方案1】:

发生的情况是对this 的引用发生了变化并指向其他内容,例如:

  • click 函数中是对触发事件的 DOM 元素的引用
  • 在each 函数中是您正在迭代的当前元素的引用

一种做法是将this 的引用保存在另一个变量中——通常称为self——这将在其他方法中作为闭包使用。为了更好地了解上下文如何变化,请查看bind、call 或apply。

同时,试试这个:

init:function(){
  this.cacheDom();      
}

cacheDom:function(){
   this.$jsTabs         = this.container.find('.js-tabs');
   this.$li             = $jsTabs.find('.tabs').find('li');
   this.$filterBodyTr   =  that.container.find('#filter_body').find("tr");
   this.$customRuleInfo = that.container.find('#custom_rule_info');
},

enableTabs :function() {
  var self = this
  this.$li.click(function(){
    var index  = self.$li.index(this);
    self.$jsTabs.tabs().select(index);
    if(index==2){
      var tr = self.$filterBodyTr;
      tr.each(function(){
        $t = $(this);
        if($t.is(":visible")){
          if ($t.text().toUpperCase().indexOf("MY NAME") > -1){
           self.$customRuleInfo.addClass("hide-data");
          }
        }
      });
    }
  });
},

【讨论】:

  • 使用此代码 this.$li 被视为未定义为 enableTabs 函数,但在 cacheDom 函数中可用。我用this.$jsTabs.find('.tabs').find('li'); 替换了$jsTabs.find('.tabs').find('li'); 我不断收到Cannot read property 'index' of undefined
  • 我告诉过你this 改变了范围。确保根据应用程序的逻辑引用当前范围。如果在click() 内,这是被点击的元素,在each 内是迭代的当前元素。确保正确使用它。 PS:我已经编辑了答案,现在试试吧!
猜你喜欢
  • 1970-01-01
  • 2018-01-25
  • 2013-03-15
  • 1970-01-01
  • 1970-01-01
  • 2012-06-12
  • 1970-01-01
  • 1970-01-01
  • 2015-04-05
相关资源
最近更新 更多