IE10: getElementsByName()不返回在IE10中具有相同Id的元素



我在jsp中有一个动态填充的表。我对所有的表行使用了相同的id。

在javascript中,我想检索其id为"resultRow"的所有表行元素。和getElementsByName("resultRow")在js给出空htmlcollection在IE10

是否有其他方法来获得匹配id

的表

任何帮助都是非常感谢的

这是我的代码片段

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();
    }
}`     

我刚刚通过我自己的测试证实,这是微软在IE10中做的一个改变,似乎是为了使它与市场上所有其他浏览器兼容。在其他浏览器中,ID和名称是不同的东西。但在IE 10之前,微软选择了不同的做法,这导致了许多不兼容,如下所述。这个改变修复了我为chrome编写的代码,但似乎已经破坏了为IE编写的代码。

如果你指示你的用户打开"兼容模式",它应该像以前一样工作,但这可能不是一个好的长期解决方案。

好消息是,如果你在代码中通过让id引用id和名称引用名称来解决这个问题,那么你的网站将与除了IE之外的其他浏览器兼容。

查看您的代码,似乎您想将id="resultRow"替换为name="resultRow"

什么时候nameid相等了?举个例子,有多少人叫约翰·史密斯(除了博士)?尽管如此,它们都有唯一的id。这同样适用于你的HTML。

使用<tr name="resultRow"...>,则可以访问getElementsByName('resultRow')

这个行为是设计出来的。
nameid是两个不同的东西。

另外, ids必须唯一;你的HTML是无效的

您应该使用class,而调用getElementsByClassName()

最新更新