2013-10-09 39 views
1
我在使用下面的代碼的麻煩

(它只是不會更改頂部屬性:S)無法更改的數組元素的風格的onClick

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(addBorder被稱爲ONCLICK上的一個div的位置):

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

看起來你有放置在按鈕的頂部等元素http://jsfiddle.net/ arunpjohny/ZUTNg/1/ –

+0

而不是使用onclick =「addBorder(event)」我用onclick =「addBorder(this)」, 確保我不改變圖像的風格,我這裏發生了什麼。 (因爲圖像是在div中,它不會移動) – Firebirdz

回答

1

你肯定是

$ctrlBeingpressed 

設置,是真的嗎?

否則這個

selectedDivs.push(e.target); 

不會執行你的

selectedDivs[0] 

將不確定

+0

而不是使用onclick =「addBorder(event)」我用onclick =「addBorder(this)」, 確保我沒有改變圖像的樣式,這是發生在這裏的事情。 (因爲圖像在div中,所以它不會移動) – Firebirdz