How do I access the parent element within a Backbo

2019-05-01 19:24发布

In Backbone model views, it seems $(this.el).parent() 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.

标签: backbone.js
2条回答
欢心
2楼-- · 2019-05-01 20:16

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()
查看更多
啃猪蹄的小仙女
3楼-- · 2019-05-01 20:20

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.

查看更多
登录 后发表回答