简体   繁体   English

Internet Explorer中的Javascript问题

[英]Javascript issue in Internet Explorer

I have a function written that searches through multiple tables looking for a device and map. 我编写了一个函数,可以搜索多个表以查找设备和地图。 Once it finds the correct cell, it returns the innerHTML to a div at the top of the page. 一旦找到正确的单元格,它就会将innerHTML返回到页面顶部的div。

The function is working perfectly in Firefox but it is not working in Internet Explorer. 该功能在Firefox中完美运行,但它无法在Internet Explorer中运行。 The function is still being run in IE but it is not finding the content from the table that it is looking for. 该函数仍在IE中运行,但它没有找到它正在寻找的表中的内容。

Here is the function: 这是功能:

   // Function for searching tables for specific map/device combination.
function findMap(map, device)
{
    var flag = false;   // to check if the map has been found yet

    // Add <strong> tags to map string so it is easier to search for
    var mapstring = "<strong>" + map;

    // create array of tables
    var tables = document.getElementsByTagName('table');

    for (var t = 2; t < tables.length; t++)
    {
        // create array of rows
        var tablerows = tables[t].getElementsByTagName("tr");
        for (var r = 2; r < tablerows.length; r++)
        {
            currentRow = tablerows[r];

            // Check if current row contains map name
            if (currentRow.innerHTML.match(mapstring))
            {

                var tablecells = currentRow.getElementsByTagName("td");
                for (var c = 1; c < tablecells.length; c++)
                {
                    currentCell = tablecells[c];
                    // Check if current cell contains device name
                    if (currentCell.innerHTML.match(device))
                    {
                        document.getElementById("boxy2").innerHTML = currentCell.innerHTML;
                        flag = true;
                    }
                    if (flag == true) return; 
                }
                if (flag == true) return; 

                // search for x20 if 920 was not found etc
                if (device == "910")
                    device = "x10";
                else if (device == "920")
                    device = "x20";
                else if (device == "930")
                    device = "x30";
                else if (device == "940")
                    device = "x40";
                else if (device == "950")
                    device = "x50";

                // search cells again
                for (var c = 1; c < tablecells.length; c++)
                {
                    currentCell = tablecells[c];
                    if (currentCell.innerHTML.match(device))
                    {
                        document.getElementById("boxy2").innerHTML = currentCell.innerHTML;
                        flag = true;
                    }
                    if (flag == true) return;
                }
                if (flag == true) return;
            }
            if (flag == true) return; else  { document.getElementById("boxy2").innerHTML="Map not available on this device."; }
        }
    }
}

Any help with this matter would be greatly appreciated. 对此问题的任何帮助将不胜感激。

Thanks. 谢谢。

I think the issue may be with this line: 我认为问题可能在于这一行:

var mapstring = "<strong>" + map;

I believe IE reports the tag names in uppercase, so this line would fail: 我相信IE以大写形式报告标签名称,因此该行将失败:

if (currentRow.innerHTML.match(mapstring))

Try making it case insensitive: 尝试使其不区分大小写:

var map_regex = new RegExp( mapstring,'i');

if (currentRow.innerHTML.match(map_regex))

(A little off topic, but it seems that you've forgotten to declare a couple variables with var unless they're somewhere else in your code. Just thought I'd mention it.) (有点偏离主题,但似乎你忘了用var声明一些变量,除非它们在你的代码中的其他地方。只是想我会提到它。)

You should try to replace tables[t].getElementsByTagName("tr") by tables[t].rows , and currentRow.getElementsByTagName("td") by currentRow.cells . 你应该尝试更换tables[t].getElementsByTagName("tr")tables[t].rowscurrentRow.getElementsByTagName("td")currentRow.cells In case you want to extend your script later, it might also be safer to give your tables an ID each and then get them via getElementById instead of getElementsByTagName . 如果您想稍后扩展脚本,为每个表分配一个ID可能更安全,然后通过getElementById而不是getElementsByTagName获取它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM