2013-05-01 29 views
0

我在jsp中有動態填充的表。 我對所有表格行都使用了相同的id。IE10:getElementsByName()沒有返回在IE10中具有相同Id的元素

在JavaScript中我想檢索所有表格行元素的id爲「resultRow」。 和getElementsByName(「resultRow」)在JS給出IE10

空的HTMLCollection是否有任何其他的方式來獲得匹配的ID

任何幫助是極大的讚賞

'這是我的代碼片段的tablerows

In JSP: 
<table id="subSecondTable" cellpadding="0" cellspacing="0"> 
    <c:set var="loopCount" value="0" /> 
    <c:forEach var="vinList" items="${requestScope.vehicleDetailsPO}"> 
    <tr id="resultRow" height="10" class="handCursor" 
     onclick="rowColorChange('<c:out value="${loopCount}"/>','<c:out value="${vinList.vin}"/>');"> 

In js: 
function rowColorChange(rowNumber,vin){ 
    var rows=document.getElementsByName("resultRow"); 
    var columns; 
    rowHighlighted=rowNumber;  
    for(i=0;i<rows.length;i++){ 
     if (i==rowNumber){  
      rows[rowNumber].style.backgroundColor="blue"; 
      //change the text color for the highlighted row 
      columns = rows[rowNumber].cells 
      for(j=0;j<columns.length;j++){ 
       columns[j].style.color = "white"; 
      } 
      //change the text color for the highlighted row 
      var previousRowint = parseInt(previousRow);     
      if (previousRow != rowNumber) 
      {    
       columns = rows[previousRowint].cells 
       for(j=0;j<columns.length;j++){ 
        columns[j].style.color = "black"; 
       } 
      }    
      previousRow=rowNumber; 
      if(vin==''){    
       vin = rows[parseInt(rowHighlighted)].cells[3].innerText; 
      } 
      document.getElementById("txtCopyVin").value=vin; 
      document.getElementById("txtCopyVin").select(); 
     }else{  
      rows[i].style.backgroundColor="white";      
     } 
    } 
    if(window.event!=null){ 
     rows[rowNumber].cells(window.event.srcElement.cellIndex).focus(); 
    } 
}`  
+0

你檢查什麼是IE10產生的HTML來取代ID =「resultRow」? – Satya 2013-05-01 17:02:31

+0

變量行是IE10中的空集合 生成的Html與Ie9中的相同 – Harish 2013-05-01 17:20:46

回答

1

此行爲是設計使然。
nameid是兩個不同的東西。

另外,id必須是唯一的;你的HTML無效。

您應該改用class,並致電getElementsByClassName()

+0

感謝getElementsByClassName()工作! – Harish 2013-05-01 17:26:35

0

從何時起nameid是否相等?舉個例子,有多少人有約翰史密斯的名字(除了醫生)?儘管如此,他們都有唯一的ID。這同樣適用於您的HTML。

使用<tr name="resultRow"...>,那麼你就可以訪問getElementsByName('resultRow')

+0

名稱不是tablerow元素的有效屬性。我用它它沒有工作 – Harish 2013-05-01 17:33:26

+0

我的印象是'name'是一個標準屬性,可以應用於所有元素。你能證明它不起作用嗎? – 2013-05-01 17:42:13

+0

對不起這是我的錯誤,它確實工作.. – Harish 2013-05-01 17:46:54

1

我剛剛與我自己的測試證實,這是一個變化,微軟似乎在IE10已經作出,使其與市場上的其他瀏覽器兼容。在其他瀏覽器中,ID和名稱是不同的東西。但在IE 10之前,微軟選擇以不同方式做事,這導致了很多不兼容性,如here所述。此更改修復了我爲Chrome編寫的代碼,但似乎破壞了爲IE編寫的代碼。

如果您指示用戶打開「兼容模式」,它應該像以前一樣工作,但這可能不是一個好的長期解決方案。

好消息是,如果您通過使id參考id和名稱引用名稱來解決此問題,那麼您的站點將與除IE以外的其他瀏覽器兼容。

看你的代碼,它似乎要與NAME =「resultRow」

相關問題