Javascript在IE8中不起作用,但在IE9和FF中都可以

时间:2013-07-17 10:13:04

标签: javascript

我有这个JS代码:

<script type="text/javascript">

function start() {
    document.forms[0].username.focus();          
        var celdas;
        var tabla;
        tabla =  document.getElementById("tabla");
        celdas = tabla.getElementsByTagName("td");
        for (var i=0; i<celdas.length; i++) { 
            if (celdas[i].innerHTML == "<b>Please Login</b>"){
                celdas[i].innerHTML = "<b>Identificación de usuario</b>"
            }
            if (celdas[i].innerHTML == "<b>Name:</b>"){
                celdas[i].innerHTML = "<b>Nombre:</b>"
            }
            if (celdas[i].innerHTML == "<b>Password:</b>"){
                celdas[i].innerHTML = "<b>Contraseña:</b>"
            }
        }

        boton =  document.getElementById("login_button");
        boton.value="Entrar";
    }

    window.onload = start;
</script>

到这个html:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<!--head, meta tags, body and other stuff--->

    <table class="list" id="tabla">
        <tr class="dark">
            <td colspan=2></td>
        </tr>
        <tr class="dark">
            <td colspan=2><b>Please Login</b></td>
        </tr>

   <!-- ETC ETC - more table stuff-->

HTML传递验证,JS在Firefox和IE9中工作,但在IE8中甚至不在IE7中。 当我逐步调试JS时,我看到IE8停在这里:

if (celdas[i].innerHTML == "<b>Password:</b>")

但请勿进入此步骤:

celdas[i].innerHTML = "<b>Contraseña:</b>"

我不是很流利的JS,所以,也许我正在做一个完全愚蠢的n00b错误......我的代码是对的吗?为什么它不起作用?

1 个答案:

答案 0 :(得分:1)

试试这个,我认为它是innerHTML

的问题
function replace_html(el, html) {
    if( el ) {
        var oldEl = (typeof el === "string" ? document.getElementById(el) : el);
        var newEl = document.createElement(oldEl.nodeName);

        // Preserve any properties we care about (id and class in this example)
        newEl.id = oldEl.id;
        newEl.className = oldEl.className;

        //set the new HTML and insert back into the DOM
        newEl.innerHTML = html;
        if(oldEl.parentNode)
            oldEl.parentNode.replaceChild(newEl, oldEl);
        else
        oldEl.innerHTML = html;

        //return a reference to the new element in case we need it
        return newEl;
    }
};

参考:http://www.jonefox.com/blog/2009/05/21/internet-explorer-and-the-innerhtml-property/