2012-02-22 36 views
0

我很困惑,爲什麼這是OT工作:JS使用變量作爲document.getElementById的ID不起作用?

var blocks = document.getElementsByTagName("td"); 
     var i = 0; 
     for(i=0;i<blocks.length;i++){ 
      var id = String(blocks[i].id); 
      var col_pos_y = document.getElementById(id).style.top; 
      alert(col_pos_y); 

     } 

我失去了一些東西,因爲Firefox的似乎只是警告了一個空白的警告信息?任何人都知道爲什麼這不起作用?

這是產生的HTML/PHP代碼TD的

<table cellspacing="0" cellpadding="0" style="position: absolute; top: 80%; z-index:5;" border="0"> 
<?php 
$blocks = array(); 
for($k=1;$k<=3;$k++){ 
    $row_number = "row_".$k; 
?> 
    <tr id="<?php echo $row_number; ?>"> 
<? 
    for($i=0;$i<=100;$i++){ 
     if($k==1){ 
      $rand_number = rand(0, 100); 
     } 
     else if($k==2){ 
      $rand_number = rand(30, 100); 
     } 
     else if($k==3){ 
      $rand_number = rand(50, 100); 
     } 
     if($rand_number<50){ 
      for($l=0;$l<=count($blocks);$l++){ 
       $row_counter = $k-1; 
       if($blocks[$l]=="row_".$row_counter."_col_".$i."_type_grass"){ 
        $block_exists="true"; 
        break; 
       } 
       else{ 
        $block_exists="false"; 
       } 
      } 
      if($block_exists=="true"){ 
       array_push($blocks, $row_number."_col_".$i."_type_grass"); 

       $background_position = generate_dirt_block(); 
       ?> 
        <td id="<?php echo $row_number; ?>_col_<?php echo $i ?>_type_grass" style="font-size: 10px; cursor: pointer; background-image:url('img/terrain.png'); background-repeat: no-repeat; background-position: <?php echo $background_position ?>; min-width: 32px; height: 32px;"> 

        </td> 
       <?php 
      } 
      else{ 
       array_push($blocks, $row_number."_col_".$i."_type_blank"); 
       ?> 
        <td id="<?php echo $row_number; ?>_col_<?php echo $i ?>_type_blank" style="font-size: 10px; min-width: 32px; height: 32px;"> 

        </td> 
       <?php 
      } 
     } 
     else if($rand_number>=50 && $rand_number<=100){ 
      array_push($blocks, $row_number."_col_".$i."_type_grass"); 
      for($l=0;$l<=count($blocks);$l++){ 
       $row_counter = $k-1; 
       if($blocks[$l]=="row_".$row_counter."_col_".$i."_type_grass"){ 
        $block_exists="true"; 
        break; 
       } 
       else{ 
        $block_exists="false"; 
       } 
      } 
      if($block_exists=="true"){ 
       $background_position = generate_dirt_block(); 
       ?> 
        <td id="<?php echo $row_number ?>_col_<?php echo $i ?>_type_grass" style="font-size: 10px; cursor: pointer; background-image:url('img/terrain.png'); background-repeat: no-repeat; background-position: <?php echo $background_position ?>; min-width: 32px; height: 32px;"> 

        </td> 
       <?php 
      } 
      else{ 
       ?> 
        <td id="<?php echo $row_number; ?>_col_<?php echo $i ?>_type_grass" style="font-size: 10px; cursor: pointer; background-image:url('img/terrain.png'); background-repeat: no-repeat; background-position: 0px -32px; min-width: 32px; height: 32px;"> 
         <div style="width: 100%; position: relative; top: 0px;"> 
          <div style="background-image:url('img/terrain.png'); background-repeat: no-repeat; background-position: -384px 0px; width: 32px; height: 10px; position: absolute; top: -17px;"> 

          </div> 
         </div> 
        </td> 
       <?php 
      } 
     } 
    } 
?> 
    </tr> 
<?php 
} 
?> 
</table> 
+1

你能發佈與此相關的HTML嗎?阿洛斯,你爲什麼在分裂後改絃樂?它似乎是將字符串重新組合起來,就像調用split()之前一樣。 – phatskat 2012-02-22 15:17:47

+0

「block_id」是否有值?你的TD ID是什麼樣的? – 2012-02-22 15:18:09

+0

你忽略了,那是我試着繞過它,但是'document.getElementById(blocks [i] .id).style.top;'由於某種奇怪的原因不起作用。我真的不能發佈整個HTML代碼,因爲它是生成這些td的php,它像100行代碼,但我會嘗試 – 2012-02-22 15:20:33

回答

4

如果getElementById()不能正常工作,然後試圖訪問.style.top的返回值將錯誤,並且alert將永遠不會達到。

元素的.style.top屬性只是空白的(表示它尚未使用JavaScript或style屬性進行設置)。

如果要訪問通過級聯應用的屬性,請使用getComputedStyle

如果要通過正常流程設置其位置,則使用offsetTop)。

+0

夢幻般的答案 - @Lenny你有沒有嘗試過,看看你是否從getElementById得到一個有效的結果?可以追逐一隻紅鯡魚。 – phatskat 2012-02-22 15:22:26

+0

是的,謝謝,這是一個愚蠢的錯誤,我想知道發生了什麼,但這解釋了它:) – 2012-02-22 15:23:21

+0

但我很好奇,有沒有辦法獲得位置,即使沒有放置屬性? – 2012-02-22 15:24:29