【问题标题】:Populating div with checkboxes用复选框填充 div
【发布时间】:2010-03-04 19:03:43
【问题描述】:

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

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 中得到了很多复选框。

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

【问题讨论】:

标签: javascript jquery html css


【解决方案1】:

保留其他内容,这会有所不同(在 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);

【讨论】:

  • 你是对的。我用天花板代替地板。您的版本显示正确。但是,如果我使用追加而不是 html。我在 FF 和 Chrome 中得到这样的图片img2.pict.com/4b/14/3a/3032352/0/clipboard2.jpg
  • 嗯,我注意到这是因为多余的空间(为什么要添加它??...)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-06-24
  • 1970-01-01
  • 2019-04-18
  • 1970-01-01
  • 2014-12-10
  • 1970-01-01
  • 2014-11-08
相关资源
最近更新 更多