2013-07-17 108 views
0

我有這樣的JS代碼:JavaScript並沒有在IE8中工作,但在IE9和FF的確

<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> 

爲了這個網站:

<!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錯誤...是我的代碼正確嗎?爲什麼它不起作用?

+0

http://stackoverflow.com/questions/1344470/why-is-document-getelementbyidtableid-innerhtml-not-工作在ie8 – DarkBee

+0

@DarkBee在我的情況下,在'​​',這是沒有mencioned在您發佈的問題... – Arkana

+0

IEHTML內部HTML問題 –

回答

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/

相關問題