简体   繁体   English

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

[英]Javascript doesn't work in IE8, but in IE9 and FF does

I have this JS code: 我有这个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>

To this html: 到此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-->

The HTML pass the validation, and the JS works in Firefox and IE9, but not in IE8 even IE7. HTML通过了验证,并且JS可在Firefox和IE9中使用,但在IE8甚至IE7中均无法使用。 When I debug the JS step by step, I see that IE8 stops at here: 当我逐步调试JS时,我看到IE8在这里停止:

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

But do not enter this step: 但不要进入此步骤:

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

I'm not very fluent in JS, so, perhaps I'm doing a completely stupid n00b error... is my code right? 我的JS不太流利,所以,也许我正在做一个完全愚蠢的n00b错误...我的代码对吗? Why it doesn't work? 为什么不起作用?

Try this, I thinks its issue with innerHTML 试试这个,我认为它与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;
    }
};

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

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

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