2010-03-04 50 views
1

我用这个代码,以确定复选框,宽度和高度:与复选框填充的div

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与恰好适合给定格复选框的数量。事实上,我得到了很多复选框。

反正是有解决这个问题?谢谢!

+0

是什么填充DIV的代码? – TheVillageIdiot 2010-03-04 19:06:52

+0

http://dl.dropbox.com/u/3473965/jq/tetris.js – Overdose 2010-03-04 19:07:56

+0

愚蠢无关的问题,复选框如何与俄罗斯方块联系起来?谢谢。 – smaclell 2010-03-04 19:32:20

回答

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); 
+1

也在Chrome中工作。 – TheVillageIdiot 2010-03-04 19:23:20

+0

你说得对。我用小区而不是地板。 您的版本显示正确。但是,如果我使用append而不是html。我在FF和Chrome中获得这样的图片http://img2.pict.com/4b/14/3a/3032352/0/clipboard2.jpg – Overdose 2010-03-04 19:30:33

+0

嗯,我注意到它发生是因为额外的空间(为什么它被添加? ...)。 – Overdose 2010-03-04 19:44:59