2009-08-15 112 views
1

請幫我選擇C ..這裏是HTML代碼:DOM導航問題,

<div id="a"> 
    <div id="b"></div> 
    <div id="app7019261521_the_coin_9996544" style="left: 176px; top: 448px;"> 
     <a href="d.com" onclick="(new Image()).src = &#039;/ajax/ct.php?d_id=;action_type=d;post_form_id=6b; return true;"></a> 
    </div> 
</div> 

這是我的Javascript代碼:

 var coin = document.querySelectorAll("a > [id^=app7019261521_the_coin]"); 
     if (coin.length == 1) { 
      alert(coin.id); 
     } else if (coin.length == 0) { 
      window.location.reload(); 
     } 

我不知道這個問題,coin.length返回1 。但是coin.id總是返回undefined,它應該返回app7019261521_the_coin_9996544

也許這是在DOM導航中。我不知道。請幫我

回答

3

它看起來像硬幣的長度爲1的數組,所以你應該使用硬幣[0] .ID

2

我不會用querySelectorAll()。嘗試像document.getElementById一樣標準的DOM行走。

也聽起來可能是數組,也許像硬幣[0]。 在帶有Web Inspector的Safari中,如果它是全局變量,則可以轉儲整個數組以查看其內容以及它在數組中的位置。

http://ejohn.org/blog/thoughts-on-queryselectorall/