English 中文(简体)
我如何在“后台观点”中获取规模(家庭/家庭)的信息?
原标题:How can I get size (height/width) information in Backbone Views?

在我的主干 app的完稿中,我想用空子来填表的其余部分,供改换头的浏览,但我知道要增加多少几行。 我尝试了<条码>。 0. 谁知道如何这样做? 我也想增加太多,以便不引入滚动酒吧。


var bar = Backbone.View.extend({
    initialize: function(){
        _.bindAll(this, "render");
    },

    render: function(){
        var html = "<h1>Hi!</h1>";
        $(this.el).html(html);
        var myWidth = this.el.width();
        alert(myWidth);
        return this;
    }

});



var foo = Backbone.View.extend({
    el: $( #myElement ),

    initialize: function(){
        _.bindAll(this, "render");
        this.subview = new bar();
    },

    render: function(){
        $(this.el).append(this.subview.render().el);
        return this;
    }

});




Solution for those stuck with the same problem as I had: You have to wait for the elements to be fully attached to the dom in order to get height/width info. In order to resolve this I added a postrender call after the render is fully done which went back and handled any small details.

最佳回答

Try getting the height after you attach it, (so in "foo" as opposed to "bar").

例如,“foo”

    render: function(){

    $(this.el).append(this.subview.render().el);
    alert($(this.subview.el).height());
    return this;
}
问题回答

在一些浏览器上,在确定html后不会立即获得宽度和高度信息。 其原因是,你更新了OMD,但浏览器可能还没有铺设新的网页。

过去,我曾以标准方式推迟呼吁,以便浏览器有时间,然后才能衡量。 您可为此使用<条码>。 例如:

render: function(){
  var html = "<h1>Hi!</h1>";
  $(this.el).html(html);
  _.defer(_.bind(function() {  
    alert($(this.el).width());
  }, this);
  return this;
}

此外, j(Q.width)可能给你比客户Width更加兼容。 亲爱!

你们的观点应当如此:

var myView = Backbone.View.extend({
    el: $( #myElement ),

    initialize: function(){
        _.bindAll(this, "render");
    },

    render: function(){
        var myHeight = this.el.height();
        alert(myHeight);
        return this;
    }

});

帮助的希望





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签