Javascript没有';在IE8中不起作用,但在IE9和FF中起作用
我有这个JS代码:Javascript没有';在IE8中不起作用,但在IE9和FF中起作用,javascript,Javascript,我有这个JS代码: <script type="text/javascript"> function start() { document.forms[0].username.focus(); var celdas; var tabla; tabla = document.getElementById("tabla"); celdas = tabla.getElementsByTagNam
<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>
函数start(){
document.forms[0]。username.focus();
瓦尔·塞尔达斯;
塔布拉变种;
tabla=document.getElementById(“tabla”);
celdas=tabla.getElementsByTagName(“td”);
对于(var i=0;i
HTML通过了验证,JS在Firefox和IE9中工作,但在IE8甚至IE7中都不工作。
当我一步一步地调试JS时,我看到IE8停在这里:
if (celdas[i].innerHTML == "<b>Password:</b>")
if(celdas[i].innerHTML==“密码:”)
但不要进入此步骤:
celdas[i].innerHTML = "<b>Contraseña:</b>"
celdas[i].innerHTML=“Contraseña:
我的JS不是很流利,所以,也许我犯了一个非常愚蠢的n00b错误……我的代码对吗?为什么它不起作用?试试这个,我认为这是
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;
}
};
参考资料:@DarkBee在我的例子中是在一个
中,这在你发布的问题中没有提到……IE8中的innerHTML问题与IE存在问题,以及它的许多版本之间的差异,这是正常的,也是意料之中的。@Arkana web开发人员的口头禅:“如果它在IE中工作,但在其他浏览器中不工作,我的代码是错误的。”
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;
}
};