代码之家  ›  专栏  ›  技术社区  ›  Overdose

用复选框填充DIV

  •  1
  • Overdose  · 技术社区  · 16 年前

    我使用此代码确定复选框的宽度和高度:

    var chk = $('input[type="checkbox"]:first');
    
    chkWidth = chk.innerWidth() 
       + parseInt(chk.css('margin-left').replace('px', '')) 
       + parseInt(chk.css('margin-right').replace('px', '')); /*IS there better way instead of px replace?? */
    
    chkHeight = chk.innerHeight()
       + parseInt(chk.css('margin-top').replace('px', '')) 
       + parseInt(chk.css('margin-bottom').replace('px', ''));
    
    fieldWidth = gamefield.innerWidth();
    fieldHeight = gamefield.innerHeight();
    

    然后我试着在DIV中填入与给定DIV完全匹配的复选框的数量。实际上,我从DIV中得到了很多复选框。

    有办法解决这个问题吗?谢谢您!

    1 回复  |  直到 16 年前
        1
  •  1
  •   TheVillageIdiot    16 年前

    如果不考虑其他因素,则会有所不同(在ff3.5中):

        var cols=Math.floor(fieldWidth/chkWidth);
    var rows=Math.floor(fieldHeight/chkHeight);
    var html='';
    for(var i=0;i<=rows;i++) //if yo use i<rows there is space for one more row
    {
        for(var j=0;j<cols;j++)
        {
            html +="<input type='checkbox'/>";
        }
    }
    
    gamefield.html(html);