类名称在IE 7,8和chrome中未更改 [英] Class Name not getting changed in IE 7, 8 and chrome

查看:112
本文介绍了类名称在IE 7,8和chrome中未更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试在onload期间更改两个元素的类名。



这里是我的代码:

  var browserName = navigator.appName; 
var tfElem = document.getElementById(TTlExpct);
var blTfElem = document.getElementById(BTLExpct);
if(browserName ==Microsoft Internet Explorer)
{
tfElem.className =pn-tf;
blTfElem.className =pn-tf active;
}
else
{
tfElem.setAttribute('class','pn-tf');
blTfElem.setAttribute('class','pn-tf active');
}

如果没有IE,else块会在mozilla中处理代码,



它不能在IE7和IE8中使用



/ p>

在Chrome中,只有在我重新载入页面时,它才会工作。



任何帮助将不胜感激。 p>

解决方案

在旧版本的IE中执行 setAttribute 。在较新的IE版本中,如果您不使用触发标准模式的Doctype,可能会仿真该错误。



替换:

  foo.setAttribute('class',value); 

有:

 code> foo.className = value; 

在这里无处不在。不要尝试进行浏览器检测。所有支持 setAttribute('class',value)的浏览器也支持 foo.className = value



问题中的代码应改写为:

  var tfElem = document.getElementById(TTlExpct); 
var blTfElem = document.getElementById(BTLExpct);
tfElem.className =pn-tf;
blTfElem.className =pn-tf active;


I am trying to change class name of two elements during onload.

Here is my code for the same :

    var browserName=navigator.appName;
    var tfElem = document.getElementById("TTlExpct");
    var blTfElem = document.getElementById("BTLExpct");
    if (browserName=="Microsoft Internet Explorer")
    {
        tfElem.className ="pn-tf";
        blTfElem.className ="pn-tf active";
        }
    else
    {
        tfElem.setAttribute('class', 'pn-tf');
        blTfElem.setAttribute('class', 'pn-tf active');
        }

The else block takes care of the code if its not IE, in mozilla, this is working fine, class name is getting set.

Its not working in IE7 and IE8

and

in chrome, it works only if i reload the page again.

Any help will be appreciated .

解决方案

There is a bug in the implementation of setAttribute in old versions of IE. In newer versions of IE that bug may be emulated if you do not use a Doctype that triggers standards mode.

Replace:

foo.setAttribute('class', value);

With:

foo.className = value;

Do this everywhere. Don't try to do browser detection. All browsers that support setAttribute('class', value) also support foo.className = value.

The code in your question should be rewritten as:

var tfElem = document.getElementById("TTlExpct");
var blTfElem = document.getElementById("BTLExpct");
tfElem.className ="pn-tf";
blTfElem.className ="pn-tf active";

这篇关于类名称在IE 7,8和chrome中未更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆