2010-08-10 120 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

相关问题