gpt4 book ai didi

jquery - 在主干中缓存 View 的子元素

转载 作者:行者123 更新时间:2023-12-01 06:50:56 25 4
gpt4 key购买 nike

我正在尝试缓存(使用 jQuery 选择器) View 中的一些子元素。

var theView = Backbone.View.extend({
initialize:function(){
this.$subEl = this.$el.find('.sub-el-class');
},
template: Handlebars.compile($('#view-template').html()),
render: function(){
this.$el.html(this.template(this.model.toJSON()));
this.$subEl.hide();
return this;
}
});

当我渲染 View 时,$subEl (.sub-el-class) 不会隐藏。我不想每次必须对 .sub-el-class 元素执行某些操作时都使用 jQuery 选择器。

有什么想法不适用于我的代码吗?

最佳答案

this.$subEl = this.$el.find('.sub-el-class');

这是一个指向 .sub-el-class 的指针,您可以在初始化 View 时设置它。

每次调用 this.$subEl 时都已准备好一个 jQuery 对象,因此它不会再次搜索 DOM。

关于jquery - 在主干中缓存 View 的子元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15022422/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com