2010-08-10 84 views
3

我使用javascript方法getElementsByTagName(「a」)來調用所有'a'標記並對它們執行一些操作。該方法適用於FF和Opera,但不適用於Chrome和Safari。 當我看在Chrome和Safari瀏覽器的調試工具,他們說:「遺漏的類型錯誤:無法調用‘的getElementsByTagName’空的」getElementsByTagName不在鉻和Safari瀏覽器中工作

這是爲什麼,什麼是修復?請有人能就此提出建議嗎?

很多預先感謝。

下面的代碼:

function popUpSAPWindow(){ 
// Find all links in the page and put them into an array. 
var linksInOrderLinesTable = document.getElementById("orderLinesTable").getElementsByTagName("a"); // The line doing the error 
var linksLen = linksInOrderLinesTable.length; 

// If the link text is 'SAP' then modify the attributes 
for(var i = 0; i < linksLen; i++){ 
    if(linksInOrderLinesTable[i].innerHTML == "SAP"){ 
     // Store the 'href' value of each SAP link. 
     var sapHref = linksInOrderLinesTable[i].href; 

     // Modify the attributes of each SAP link.  
     linksInOrderLinesTable[i].setAttribute("href", "javascript:return false;"); 
     linksInOrderLinesTable[i].setAttribute("onclick", "sapNewWindow(\'" + sapHref + "\')"); 
    } 
} 

}

它的工作原理與此HTML:

<table id="orderLinesTable" summary="List of orders made by customers that the administrator can pick and deal with"> 
<tr> 
    <th>Status</th> 
    <th>Basket id</th> 
    <th>Order line id</th> 
    <th>Product</th> 
    <th>Company</th> 
    <th>Catalogue</th> 
    <th>Date</th> 
    <th>Details</th> 
</tr> 
<tr> 
    <td>Accepted</td> 
    <td>236569</td> 
    <td>207</td> 
    <td>OS Master Map</td> 
    <td>NHS</td> 
    <td>Standard</td> 
    <td>1 Aug 10</td> 
    <td><a href="/orderLineDetails.html">Normal</a> <a href="/orderLineDetails.html">SAP</a></td> 
</tr> 
<tr> 
    <td>New</td> 
    <td>236987</td> 
    <td>528</td> 
    <td>Code-Point</td> 
    <td>BT</td> 
    <td>Standard</td> 
    <td>9 Aug 10</td> 
    <td><a href="/orderLineDetails.html">Normal</a> <a href="/orderLineDetails.html">SAP</a></td> 
</tr> 

但是,當我在其他網頁它給提到的錯誤。

+0

該錯誤表明它不是真正的方法本身,而是你要調用它的對象,它是空的(因此不具有法) 。你能發佈你的JavaScript代碼嗎? – Rob 2010-08-10 11:30:31

+0

好吧,我現在將它發佈,看問題本身。 – Shaoz 2010-08-10 11:59:01

回答

3

問題是,當您在沒有orderLinesTable的頁面上調用document.getElementById("orderLinesTable").getElementsByTagName("a")時,getElementById將返回null。因此在null上調用getElementsByTagName將產生錯誤。

這應該解決的問題:

var orderLinesTable = document.getElementById("orderLinesTable"); 
var linksInOrderLinesTable = []; 

if (orderLinesTable) { // only get the links when the table exists 
    linksInOrderLinesTable = orderLinesTable.getElementsByTagName("a"); 
} 
+0

你的代碼和if語句一起工作正常,非常感謝,但Chrome現在說:「資源解釋爲腳本,但是以MIME類型text/plain傳輸。」 我知道我所有的腳本標記都具有MIME類型=「tetx/javascript」。怎麼了Chrome及其過敏引擎?您是否曾經遇到過這個消息? – Shaoz 2010-08-12 11:54:07

+0

如果您通過'src'屬性包含您的腳本,Chrome可能會提醒您有關您的服務器不會正確設置「內容類型」標頭。 – 2010-08-12 21:41:03

1

Safari和Chrome都支持該方法。您使用它的對象可能無法一致檢索,因此它的計算結果爲空。檢查你是如何抓取你打電話給它的對象的。

順便說一句。它不是一個JavaScript方法,它是一個在DOM API中的方法。

編輯:

document.getElementById("orderLinesTable") 

警告這是什麼。它是否爲空?

+0

是的,這將是空的,因爲這段代碼是針對特定頁面的那個「orderLinesTable」id。它只顯示沒有獲得該ID的其他頁面上的錯誤。問題是,JavaScript文件是整個Web應用程序的包含文件。其他頁面不應該能夠實現代碼,因爲它不涉及它們,這是FF和Opera正在做的事情,他們忽略它。那麼,爲什麼他們打擾了Chrome和Safari呢? 順便說一句:感謝您對DOM API的修正 – Shaoz 2010-08-10 13:14:46

+0

我剛剛檢查過警報,它說它是空的,那麼我該怎麼辦?我卡... – Shaoz 2010-08-11 14:32:04

+0

提供HTML。並告訴我們該函數何時被調用。還有其他你沒有說的東西。 – 2010-08-11 15:12:43

相關問題