我使用这段代码来确定复选框的宽度和高度:
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 中得到了很多复选框。
有没有办法解决这个问题?谢谢!
最佳答案
保留其他内容,这会有所不同(在 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);
关于javascript - 用复选框填充 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2381967/