3

我有一个简单的 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("class", "singlebutton") 或将 className 存储在字符串中并使用 elTdText.className = classNameVar 但没有效果。除非我添加看起来很愚蠢的 className = className,否则它不起作用。

4

1 回答 1

0

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

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

elTdText.className = elTdText.className;

A = A 什么都不做。

于 2013-06-20T14:22:02.367 回答