繁体   English   中英

无法使用onClick更改数组元素的样式

[英]Cannot change style of an array element with onClick

我在执行以下代码时遇到麻烦(它不会更改top属性:s)

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 test(){
  alert('changing position');
   selectedDivs[0].style.top="10px";
  }


<button type="button" onclick="test()">Right</button>

一些生成的HTML(在其中一个DIV的Onclick中调用addBorder):

<!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>

你确定是

$ctrlBeingpressed

设置了,是真的吗?

否则这个

selectedDivs.push(e.target);

不会执行,您

selectedDivs[0] 

将是不确定的

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM