Javascript document.getElementById("...").className = "..."; 不能在 IE 中工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8143370/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 04:51:39  来源:igfitidea点击:

document.getElementById("…").className = "…"; Not working in IE

javascriptinternet-explorergetelementbyidclassname

提问by Mcestone

I created a business generator here: http://minespress.net/web_apps/business-cards/

我在这里创建了一个业务生成器:http: //minespress.net/web_apps/business-cards/

I created a preview pane using document.getElementById("…").className = "…";which switches the background image onClick (of the thumbnail) and also moves around the text divs depending on the design of the business card. This works flawlessly in FF, Chrome and Safari. When I tried it in IE nothing happened onClick.

我创建了一个预览窗格,使用document.getElementById("…").className = "…";它切换背景图像 onClick(缩略图)并根据名片的设计在文本 div 周围移动。这在 FF、Chrome 和 Safari 中完美无缺。当我在 IE 中尝试时,onClick 没有任何反应。

I should also mention that my thumbs are radio buttons that need to change the form action based whether or not they are checked. Once again works fine in other browsers besides IE. Is there some IE bug or am I just doing something completely wrong?

我还应该提到,我的拇指是单选按钮,需要根据是否选中它们来更改表单操作。再次在 IE 之外的其他浏览器中正常工作。是否有一些 IE 错误或者我只是在做一些完全错误的事情?

Here is an example of my code:

这是我的代码示例:

<input
    style="display:none"
    checked="checked"
    name="group1"
    id="2"
    value='http://minespress.net/web_apps/business-cards/img/twinkle-blue.jpg'
    type="radio"
    onclick="document.getElementById('card').className ='Default'; document.getElementById('compname').className ='', document.getElementById('slogan').className ='', document.getElementById('leftinfo').className ='', document.getElementById('rightinfo').className =''" />

采纳答案by Mcestone

IE 8 does not support images as labels for input fields. This is why my radio buttons do not work. Now I just need to figure out how to work around this.

IE 8 不支持图像作为输入字段的标签。这就是我的单选按钮不起作用的原因。现在我只需要弄清楚如何解决这个问题。

回答by Diodeus - James MacFarlane

Use setAttribute instead:

使用 setAttribute 代替:

var ele = document.getElementById('card')
ele.setAttribute('class', 'Default');

回答by Dennis

Did you try changing the ,s to ;s? IE might be pickier than the other browsers...

您是否尝试将,s更改为;s?IE 可能比其他浏览器更挑剔...

Change

改变

"document.getElementById('card').className ='Default'; document.getElementById('compname').className ='', document.getElementById('slogan').className ='', document.getElementById('leftinfo').className ='', document.getElementById('rightinfo').className =''"

"document.getElementById('card').className ='Default'; document.getElementById('compname').className ='', document.getElementById('slogan').className ='', document.getElementById('leftinfo').className ='', document.getElementById('rightinfo').className =''"

to

"document.getElementById('card').className ='Default'; document.getElementById('compname').className =''; document.getElementById('slogan').className =''; document.getElementById('leftinfo').className =''; document.getElementById('rightinfo').className =''"

"document.getElementById('card').className ='Default'; document.getElementById('compname').className =''; document.getElementById('slogan').className =''; document.getElementById('leftinfo').className =''; document.getElementById('rightinfo').className =''"

and see if that solves your problem.

看看这是否能解决你的问题。