Javascript 在if语句中更改classname没有视觉效果,但属性会更改
我有一个简单的html+javascript屏幕,其中有一些按钮在运行时连接到屏幕上 它以前在IE6中工作,但现在平台更新为Windows7+IE9(在IE8 comp模式下),屏幕被全部扰乱 这是该功能的一部分Javascript 在if语句中更改classname没有视觉效果,但属性会更改,javascript,html,css,dom,classname,Javascript,Html,Css,Dom,Classname,我有一个简单的html+javascript屏幕,其中有一些按钮在运行时连接到屏幕上 它以前在IE6中工作,但现在平台更新为Windows7+IE9(在IE8 comp模式下),屏幕被全部扰乱 这是该功能的一部分 function AttachButton(btn, buttonloc) { var elTr = document.createElement('TR'); var elTdText = document.createElement('TD'); elTdT
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";
}
}
功能附加按钮(btn,按钮LOC)
{
var elTr=document.createElement('TR');
var elTdText=document.createElement('TD');
elTdText.id=idbase+“text”;
elTr.appendChild(elTdText);
//*剪断*
//下面是将TR附加到正确表的代码
//******
if(elTdText.offsetWidth<162&&btnText.search(“
”)<0){
elTdText.className=“singlebutton”;
}
否则{
elTdText.className=“doublebutton”;
}
如果(btn.name==“联系人”){
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;
}
功能附加按钮(btn,按钮LOC)
{
var elTr=document.createElement('TR');
var elTdText=document.createElement('TD');
elTdText.id=idbase+“text”;
elTr.appendChild(elTdText);
//*剪断*
//下面是将TR附加到正确表的代码
//******
if(elTdText.offsetWidth<162&&btnText.search(“
”)<0){
elTdText.className=“singlebutton”;
}
否则{
elTdText.className=“doublebutton”;
}
如果(btn.name==“联系人”){
elTdText.className=“contactbutton”;
}
elTdText.className=elTdText.className;
}
我尝试将代码更改为使用setAttribute(“class”,“singlebutton”)或将className存储在字符串中,并使用elTdText.className=classNameVar,但没有效果。
除非我添加了看起来愚蠢的className=className,否则它不会工作。据我所知,className是IE特有的。尝试使用“class”或
setAttribute('class',…)
编辑尝试后,错误可能是因为:
elTdText.className = elTdText.className;
A=A什么都不做。在其他浏览器中会发生什么?这是您的代码还是IE的问题?我猜您正在更改
tr
的背景(颜色),该背景由td
的背景覆盖。但我仍然无法想象在执行eldtext.className=eldtext.className代码>。我们能看到相关的CSS吗?我不能在其他浏览器上测试它。它就像一种信息亭,里面有太多的IE特定(遗留)代码,也许我应该描述一下哪里出了问题。该类只对文本进行定位和样式设置。在没有代码的情况下,文本位于它应该位于顶部的图像旁边。这些类名设置颜色、高度、换行字、顶部、左侧、位置、zindex和fontfamily。主要的变化是将位置设置为结果,我想我看到,在版本开发的某个点上,为td
中的元素设置position:absolute
,使用table
作为定位的父元素,旧版本使用td
,即td
内的绝对定位是不可能的。什么className
是所有特定于浏览器的属性名。现在是扩展;)的时候了。也许你应该再看看我的问题。我知道这是一行愚蠢的代码,但除非a=a在那里,否则它就不能工作。然而,当我在该行之前发出警报时,该属性是正确的。如果我可以反复阅读,我永远不会理解您为什么要执行elTdText.className=elTdText.className代码>以及为什么这样做会改变任何事情:o@Teemu如果成员名称不再匹配属性名称(即id->id,class->className),则该名称不再一致,可能导致错误和不直观的代码等,并且不应再使用。