IE7和setAttribute()删除类 [英] IE7 and setAttribute() to remove classes

查看:148
本文介绍了IE7和setAttribute()删除类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个非常有趣的问题. 我正在写一个插件,允许您放大/缩小图像.我有3个按钮:关闭(关闭窗口"),放大,缩小. 放大/缩小按钮也具有禁用版本.当您达到最小/最大缩放量时,它会被激活.

如果打开图片进行缩放,则会看到一个活动的缩小按钮和一个禁用的放大按钮(因为我在打开时设置了最大值).当您第一次单击缩小"按钮时,放大"按钮应摆脱禁用的类. 它可以在Safara,Chrome,Firefox 3.6/4/5,IE8中正常运行,而在IE7中则不能.

放大"按钮具有ID和类,我想强制IE7从元素中删除特定的类.首先,我使用了removeClass(),但是没有用.然后,我使用setAttribute(),该函数可在IE7以外的所有浏览器中使用.

这里是示例. 因此,当您打开图像进行缩放时,缩小按钮具有ID ="zoom-button-in"和5类:zoom-icon,small-icon-small,zoom-button-in,zoom-button-disable,禁用放大按钮. 我想删除2个禁用"类.所以我用这个:

var elementZoomButtonIn = document.getElementById("zoom-button-in");
elementZoomButtonIn.setAttribute("class", "zoom-icon zoom-icon-small zoom-button-in");

在插入非禁用类之前,我尝试将类设置为空,但是没有用.

此方法在IE7中有效吗? (-:

谢谢你们!

解决方案

setAttribute()getAttribute()通常在IE 7和更早版本(以及更高版本中的兼容模式)中损坏.请使用元素的className属性:

elementZoomButtonIn.className = "zoom-icon zoom-icon-small zoom-button-in";

即使setAttribute()getAttribute()在IE中没有损坏,但使用等效的DOM属性代替通常更容易,更可靠.

I've got a pretty interesting issue. I'm writing a plugin which allows you to zoom in/out an image. I've got 3 buttons: close (close the 'window'), zoom in, zoom out. The buttons zoom in/out have got disabled versions, too. Its activated when you reach the minimum/maximum amount of zoom.

If you open the picture to zoom, you can see an active zoom out button, and a disabled zoom in button (because I set the maximum value at opening). When you first clicked on the zoom out button, the zoom in button should get rid of disabled class. It works fine in Safara, Chrome, Firefox 3.6/4/5, IE8 but not in IE7.

The zoom in button has an ID and classes and I want to force IE7 to remove specific classes from the element. First, I used removeClass(), but it didn't work. Then I use setAttribute(), which works in every browser but IE7.

Here is the example. So, when you open the image to zoom, the zoom out button has ID="zoom-button-in" and 5 classes: zoom-icon, zoom-icon-small, zoom-button-in, zoom-button-disabled, zoom-button-disabled-in. And I want to remove the 2 'disabled' classes. So I use this:

var elementZoomButtonIn = document.getElementById("zoom-button-in");
elementZoomButtonIn.setAttribute("class", "zoom-icon zoom-icon-small zoom-button-in");

I've tried to set the class empty before inserted the non-disabled classes, but didn't work.

Is this method working in IE7? (-:

Thank you, Guys!

解决方案

setAttribute() and getAttribute() are generally broken in IE 7 and earlier (and compatibility modes in later versions). Use the element's className property instead:

elementZoomButtonIn.className = "zoom-icon zoom-icon-small zoom-button-in";

Even if setAttribute() and getAttribute() weren't broken in IE, it's still generally easier and more reliable to use equivalent DOM properties instead.

这篇关于IE7和setAttribute()删除类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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