简体   繁体   English

如何在呈现后立即绑定HTML元素

[英]How to bind a HTML element as soon as it is rendered

I have a span in my view which is rendered using Backbone.js 我的视图中有一个跨度,该跨度是使用Backbone.js渲染的

I want to get its HTML as soon as its rendered 我想在渲染后立即获取它的HTML

Something like : $(span).html() 像这样的东西: $(span).html()

How can I do this? 我怎样才能做到这一点?

This question confused me a bit, because Backbone.View doesn't actually have a render method at all (well, technically it does but its a no-op); 这个问题使我有些困惑,因为Backbone.View实际上根本没有任何render方法(从技术上讲,它是无操作的)。 it's 100% user-defined. 它是100%用户定义的。 Given that fact, checking ... well, anything after you render is as simple as ... well, checking it after you render. 鉴于这一事实,检查...好,渲染后的所有内容都非常简单... ...好,渲染后进行检查。

In other words, if your view's render method is: 换句话说,如果视图的render方法是:

var YourView= Backbone.View.extend({
    render: function() {
        this.$el.html(someHtml);
    }
});

Then all you need to do is: 然后,您需要做的就是:

var YourView= Backbone.View.extend({
    render: function() {
        this.$el.html(someHtml);
        console.log(this.$el.html()); // check rendered HTML
    }
});

If you want to intermediate that with events you could (as @aerodynamo suggested): 如果您想通过事件进行中介(如@aerodynamo建议):

var YourView= Backbone.View.extend({
    events: {'customPostRender': 'postRender'},
    postRender: function() {
        console.log(this.$el.html()); // check rendered HTML
    },
    render: function() {
        this.$el.html(someHtml);
        this.trigger('customPostRender');
    }
});

but really that's not even necessary. 但实际上甚至没有必要。

不确定MutationObserver是否有帮助,但绝对值得一试:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM