javascript - IE10 : getElementsByName() is not returning the elements that has same Id in IE10

标签 javascript internet-explorer-10 getelementsbyname

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

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

有没有其他方法可以获取匹配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();
    }
}`     

最佳答案

我刚刚通过自己的测试证实,这是 Microsoft 似乎在 IE10 中所做的更改,以使其与市场上所有其他浏览器兼容。在其他浏览器中,ID 和名称是不同的。但在 IE 10 之前,微软选择以不同的方式做事,这导致了很多不兼容性,如here所述。 。此更改修复了我为 chrome 编写的代码,但似乎破坏了为 IE 编写的代码。

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

好消息是,如果您在代码中通过使 id 引用 id 和名称引用名称来解决并解决此问题,您的网站将与 IE 之外的其他浏览器兼容。

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

关于javascript - IE10 : getElementsByName() is not returning the elements that has same Id in IE10,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16322770/

相关文章:

excel - 为什么 "get Elements By Name"不起作用?

javascript - 如何在没有辅助方法的情况下从 Rails Asset Pipeline 获取 .swf 文件?

javascript - 停止对象的 js 间隔

html - 我如何强制 IE *不* 使用兼容模式?

windows-7 - 在 Windows 7 上安装 IE10 后如何修复 Visual Studio 2012?

javascript - 如何让 ImpressJS 在 IE10 上工作?

javascript - Vanilla JS - 通过 div 的名称获取元素

javascript - 使用纯 JavaScript 创建对此 HTML 元素的引用的最有效方法是什么?

javascript - 使用 Backbone.js 是否需要使用表单标签?

javascript - p5.j​​s - 随机()、高度和宽度未定义?