开发者

How do I access the parent element within a Backbone view?

开发者 https://www.devze.com 2023-03-05 06:06 出处:网络
In Backbone model views, it seems $(this.el).par开发者_JS百科ent() doesn\'t work.What is the best way to select the parent element from within a view?

In Backbone model views, it seems $(this.el).par开发者_JS百科ent() doesn't work. What is the best way to select the parent element from within a view?

I am setting el by using tagName: "li" for the view.


By default, Backbone assigns an empty div to your view and you can't access its parent, until it's been placed in the DOM through your render function.

You can use your approach of $(this.el).parent() if you are allowing Backbone to assign the default empty div. You can use the this.el.parent() if you are assigning el yourself in the constructor to a jQuery object.


What are you setting this.el to? If you're already setting it to a jquery element, for example:

this.el = $('#content');

then you don't need to wrap it again. Try changing what you have to:

this.el.parent()
0

精彩评论

暂无评论...
验证码 换一张
取 消