如何在一个td内浮动div

时间:2013-01-11 09:59:53

标签: javascript css html css-float html-table

我有两个页面,几乎相同的代码。两者都有这个CSS代码和两个div:

<style type="text/css">
.visfarve { position:relative; float:left; padding:5px; border:1px solid #666; }
.farver { border:1px solid black; width:75px; height:10px; }
.valgtfarve { width:95px; height:15px; display:block; background-color:white; text-align:center; }
</style>

<div id="vis" style="display:block; font-size:11px;">
  <a href="javascript:colors(1);">Vis farver</a>
</div>
<div id="skjul" style="display:none; font-size:11px;">
  <a href="javascript:colors(0);">Skjul farver</a>
</div>

然后一页有这个结构:

<table style="width:730px;">
  <tr>
    <td width="510px">
      <div id="visfarve" style="display:none; margin-top:11px;">&npsp;</div>
    </td>
    <td id="viser" style="border-radius:10px; width:220px; height:inherit"></td>
  </tr>
</table>

另一页有这个障碍:

<div id="visfarve" style="display:none; margin-top:11px;">&nbsp;</div>
<div id="viser" style="margin-top:50px; border-radius:10px; display:none;">&nbsp;</div>

他们分享这两个js函数:

var farve1 = ["3D3D34","463834","3F383D","433839","333F44","3B3B40","38413D","304344","483C33","313F48","37463B","35453F"];
var farve2 = ["S8505-Y20R","7812-Y87R","S8010-R30B","S8010-R10B","8108-R93B","8207-R38B","S8502-G","S8010-B30G","S8010-Y70R","S8010-R90B","S8010-G10Y","S8010-B90G"];

function colors(vis) {
  var setheight = 0;
  for(i=farve1.length-1;i>-1;i--) { skriv += "<div class='visfarve' onmouseover=\"document.getElementById('viser').style.background = '#" + farve1[i] + "';\" onmouseout=\"document.getElementById('viser').style.background = 'white';\" onclick='valgtfarve(\"" + farve2[i] + "\",\"#" + farve1[i] + "\");'><table><tr><td class='farver' title='Klik for at vælge' style='background-color:#" + farve1[i] + ";'></td></tr><tr><td><p>" + farve2[i] + "</p></td></tr></table></div>"; setheight += 10; }
  document.getElementById('visfarve').innerHTML = skriv;

  if(vis) { 
    document.getElementById('vis').style.display = "none";
    document.getElementById('skjul').style.display = "block"; 
    document.getElementById('visfarve').style.display = "block";
    document.getElementById('viser').style.display = "block";
    document.getElementById('viser').style.height = setheight + "px";
  }
  else {
    document.getElementById('vis').style.display = "block";
    document.getElementById('skjul').style.display = "none";
    document.getElementById('visfarve').style.display = "none";
    document.getElementById('viser').style.display = "none";
    document.getElementById('viser').style.height = 0 + "px";
  }
}

function valgtfarve(kode, farve) {
  if(!valgt) { 
    if(confirm("Tones en maling kan den ikke returneres.\nAccepter fraskrivelse af returret for tonet maling?")) { valgt = true; } 
    else {
      document.getElementById('vis').style.display = "block";
      document.getElementById('skjul').style.display = "none";
      document.getElementById('viser').style.height = "0px";
      document.getElementById('visfarve').style.display = "none"; return false;
    }}
  document.getElementById('valgtfarve').value = kode;
  document.getElementById('valgtfarve').style.backgroundColor = farve;
}

当我使用div构造时,浮动divs彼此相邻,并且每行中有5个div,并在到达容器div边缘时断开。但是,在td内部,divs坚持每个都有td的全宽 - 每行只有一个div。

如何在td中获得div以保持宽度并向左浮动?

1 个答案:

答案 0 :(得分:1)

让我们从jsFiddle链接开始:Here

我认为发布问题时这是一个很好的做法。

请解释问题的位置,在jsfiddle页面中我没有看到你的错误。

我只做了很少的修改,没有相关的代码更改。 我只定义了变量

var skriv = "";