Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
110 views
in Technique[技术] by (71.8m points)

javascript - IE8 v8 not changing class for a DOM element despite JS function changing the element attribute

I have an on-screen keyboard in order to provide a safer input for passwords.

The keyboard itself is placed like this:

<div class="teclado_grafico" id="teclado_grafico">
    <a class="tecla_teclado"  onmousedown="teclaAction( this, 'caja_selector'); return false" style="top: 0px; left: 0px;">Q</a>
    <a class="tecla_teclado"  onmousedown="teclaAction( this, 'caja_selector'); return false" style="top: 0px; left: 28px;">W</a>
.
.
.
</div>

And it has a "Shift button" which fires a JS function with this (I've already tried all that, indeed):

if (obj.innerHTML == "Mayus.") {
    try {
        MAYUSCULA_ACTIVADO = !MAYUSCULA_ACTIVADO;
        var tgrafico = document.getElementById("teclado_grafico");
        if(MAYUSCULA_ACTIVADO) {
//            tgrafico.className = "teclado_grafico mayuscula";
//            $("#teclado_grafico").removeClass('minuscula').addClass('mayuscula');
//            $("#teclado_grafico").attr('class', 'teclado_grafico mayuscula');
//            $("#teclado_grafico").attr('className', 'teclado_grafico mayuscula');
            tgrafico.setAttribute('className', "teclado_grafico mayuscula") ||
            tgrafico.setAttribute('class', "teclado_grafico mayuscula");
        } else {
//            tgrafico.className = "teclado_grafico minuscula";
//            $("#teclado_grafico").removeClass('mayuscula').addClass('minuscula');
//            $("#teclado_grafico").attr('class', 'teclado_grafico minuscula');
//            $("#teclado_grafico").attr('className', 'teclado_grafico minuscula');
            tgrafico.setAttribute('className', "teclado_grafico minuscula") ||
            tgrafico.setAttribute('class', "teclado_grafico minuscula");
        }
    } catch (_E) {
        //void
    }
    return;
}

The associated CSS is like this:

.mayuscula a.tecla_teclado{
    text-transform: uppercase;
}
.minuscula a.tecla_teclado{
    text-transform: lowercase;
}

It works on every single browser I've tried. IE 6, 7; Opera 10; GChrome; FF 3, 3.5 and 3.6; Safari 4,... but in IE8 v8 (strict mode) the class is not changed! I mean, debuggin' with the IE8 tools allows one to see that the attribute className is there and it changes... but the user does not see the letters changing from uppercase to lowercase, to uppercase again.

I just don't know how to handle this... I had complains about the client using IE6... now they updated their stuff and this shows up. Any help will be reaaaaly helpful!

EDIT Already tried suggestions of

tgrafico.className = MAYUSCULA_ACTIVADO ? "teclado_grafico mayuscula" : "teclado_grafico minuscula";

but no joy yet. Opening IE8 dev's tools allows one to see in the HTML tab that the class is changing correctly between the expected values, but the browser just does not behave!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Don't go anywhere near attributes for this, since IE's behaviour with attributes is inconsistent with other browsers and across modes and you don't need to deal with them. Instead, just use the element's className property:

tgrafico.className = MAYUSCULA_ACTIVADO ?
                     "teclado_grafico mayuscula" : "teclado_grafico minuscula";

UPDATE

This appears to be a bug in IE 8. The approach is definitely correct, and the class is getting switched: you can prove this by changing the appropriate CSS and observing the text color changes correctly when you click the shift button:

.mayuscula a.tecla_teclado{
    text-transform: uppercase;
    color: green;
}
.minuscula a.tecla_teclado{
    text-transform: lowercase;
    color: blue;
}

Furthermore, each <a> element's currentStyle.textTransform property is being set correctly, as you can prove using the following:

<a class="tecla_teclado" onclick="alert(this.currentStyle.textTransform);">Q</a>

So we conclude it's a rendering bug in IE 8. I've found a workaround, which is not to apply text-transform on the default state, which is upper case. So using my class-switching code and changing your CSS to

.mayuscula a.tecla_teclado{
}
.minuscula a.tecla_teclado{
    text-transform: lowercase;
}

... will fix it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...