更改if语句中的classname没有视觉效果但属性更改

时间:2013-06-20 14:19:16

标签: javascript html css dom classname

我有简单的html + javascript屏幕,其中在运行时屏幕上附有按钮 它曾经在IE6中工作,但现在平台更新到Windows 7 + IE9(在IE8 comp模式下)并且屏幕全部都被打乱了。

这是该功能的一部分

function AttachButton(btn, buttonloc)
{
    var elTr = document.createElement('TR');
    var elTdText = document.createElement('TD');
    elTdText.id= idbase+"text";

    elTr.appendChild(elTdText);
    //*snip*
    //here was code that attaches the TR to the proper table
    //******
    if (elTdText.offsetWidth < 162 && btnText.search("<br>") < 0) {
        elTdText.className = "singlebutton";
    }
    else {
        elTdText.className = "doublebutton";
    }
    if (btn.name == "contact") {
        elTdText.className = "contactbutton";
    }
}

这似乎没有效果。如果我删除这部分代码,页面具有相同的样式。 但是每当我在这个函数的末尾添加className = className时,一切都恢复正常 像这样:

function AttachButton(btn, buttonloc)
{
    var elTr = document.createElement('TR');
    var elTdText = document.createElement('TD');
    elTdText.id= idbase+"text";

    elTr.appendChild(elTdText);
    //*snip*
    //here was code that attaches the TR to the proper table
    //******
    if (elTdText.offsetWidth < 162 && btnText.search("<br>") < 0) {
        elTdText.className = "singlebutton";
    }
    else {
        elTdText.className = "doublebutton";
    }
    if (btn.name == "contact") {
        elTdText.className = "contactbutton";
    }
    elTdText.className = elTdText.className;
}

我尝试更改代码以使用setAttribute(&#34; class&#34;,&#34; singlebutton&#34;)或将className存储在字符串中并使用elTdText.className = classNameVar但无效。 除非我添加看起来很愚蠢的className = className,否则它不起作用。

1 个答案:

答案 0 :(得分:0)

据我所知,className是IE特有的。尝试使用“class”或setAttribute('class', ...)

编辑,因为您尝试过,错误可能是因为:

elTdText.className = elTdText.className;

A = A什么都不做。