0

我在 DIV 中有一些图像,我想给所有相同的 Top 值作为数组中的第一个值(以便它们在一行上对齐)。

我在使用以下代码时遇到问题:(单击 div 时调用 addBorder)

<div class="DRAGGABLE ui-widget-content ui-draggable" style="border: 1px solid red; width: 120px; height: 60px; top: 10px; left: 10px; position: relative;" onclick="addBorder(event)"> <img id="PMF01" src="/devices/AAU01-010.gif"></img></div>

var selectedDivs = new Array();
function addBorder(e) {
      if ($ctrlBeingpressed == true) {
      e.target.style.border="5px solid green"; // to show they are selected
      selectedDivs.push(e.target);
      }
  }

 function orderTop(){
  for (i=0;i<selectedDivs.length;i++)
    {
    selectedDivs[i].style.top=selectedDivs[0].style.top();
    }
  }

<button type="button" onclick="orderTop()">Top</button>

生成的 HTML:

<!DOCTYPE html>
<html lang="en">

    <head> … </head>
    <body style="position: absolute; cursor: auto;" application"="" onkeyup="clearPress()> <div id=" onkeydown="controlCheck(event)">
        <div id="accordion" class="ui-accordion ui-widget ui-helper-reset" style="position:absolute;top:0px;left:0px;width:300px;height:900px" role="tablist"> … </div>
        <div id="buttons" style="position:absolute;top:0px;left:315px;width:1100px;height:50px;border:2px solid grey"> … </div>
        <div id="canvas" class="DROPPABLE ui-droppable" style="position:absolute;top:50px;left:315px;width:1100px;height:850px;border:2px solid grey">
            <div class="DRAGGABLE ui-widget-content ui-draggable" style="border: 1px solid red; width: 120px; height: 60px; top: 156.117px; left: 188.117px; position: relative;" onclick="addBorder(event)">
                <img id="PMF00" src="/devices/AAU01-010.gif" style="border: 5px solid green;"></img>
            </div>
            <div class="DRAGGABLE ui-widget-content ui-draggable" style="border: 1px solid red; width: 120px; height: 60px; top: 10px; left: 10px; position: relative;" onclick="addBorder(event)">
                <img id="PMF01" src="/devices/AAU01-010.gif"></img>
            </div>
        </div>
    </body>

</html>
4

2 回答 2

0

设置顶部样式时,您需要包含单位(ems、px、% 等)。试试这条线:

selectedDivs[i].style.top = selectedDivs[0].style.top + 'px';
于 2014-02-10T11:08:01.383 回答
0

top不是函数。你不能叫它。删除().

于 2013-10-09T10:00:40.460 回答