简体   繁体   English

在IE6、8和Firefox中删除样式属性

[英]Removing Style Attribute in IE6, 8, and Firefox

I'm attempting to work with Javascript through a cross browser platform that is required to support IE6, 8, and Firefox. 我正在尝试通过支持IE6、8和Firefox的跨浏览器平台使用Javascript。 I've quickly discovered that none of these browsers contain a matching Javascript library. 我很快发现这些浏览器都不包含匹配的Javascript库。

The goal is to have items that dynamically hide or show depending on the selection of other items. 目标是使项目根据其他项目的选择动态隐藏或显示。 Normally I would just toggle between display:none and display:block but for the work of another developer, I can use the display:none to hide the field, but switching to display:block screws up the layout. 通常,我只是在display:none和display:block之间切换,但是对于另一个开发人员,我可以使用display:none隐藏该字段,但是切换到display:block会破坏布局。 The solution is to simply rip out either the display setting in the style, or tear out the style altogether. 解决方案是简单地撕掉样式中的显示设置,或完全撕掉样式。 Unfortunately, I ran into the library problem 不幸的是,我遇到了图书馆问题

Firefox supports everything I've tried so far IE8 & 6 didn't support getElementById().style.removeProperty('display') IE6 doesn't support getElementById().removeAttribute('style') Firefox支持到目前为止我尝试过的所有功能IE8和6不支持getElementById()。style.removeProperty('display')IE6不支持getElementById()。removeAttribute('style')

Below is my code as it currently is, working in IE8 and FF...but it is required to also get it working in IE6. 下面是我目前在IE8和FF中工作的代码...但是也需要使其在IE6中工作。

function displayPrevLPQ(bShow) {
    if (bShow) {
        document.getElementById('prevLPQ').removeAttribute('style');
    } else {
        document.getElementById('prevLPQ').style.display = 'none';
    }
}

function displayBusUnitSub() {
    var buVal = document.getElementById('BusinessUnitID').value;
    document.getElementById("BusinessCycle").selectedIndex = document.getElementById("BusinessCycle").getAttribute("default");
    document.getElementById("Ibap").selectedIndex = document.getElementById("Ibap").getAttribute("default");
    document.getElementById("Bca").selectedIndex = document.getElementById("Bca").getAttribute("default");

    switch (buVal) {
        case '11':
            document.getElementById('buSub0').style.display = 'none';
            document.getElementById('buSub1').removeAttribute('style');
            document.getElementById('buSub2').style.display = 'none';
            break;
        case '1':
            document.getElementById('buSub0').style.display = 'none';
            document.getElementById('buSub1').style.display = 'none';
            document.getElementById('buSub2').removeAttribute('style');
            break;
        default:
            document.getElementById('buSub0').removeAttribute('style');
            document.getElementById('buSub1').style.display = 'none';
            document.getElementById('buSub2').style.display = 'none';
            break;
    }
}

So, the question is...how can I tear out the Style or display properties in a way that will work across all three browsers? 因此,问题是...如何以一种可在所有三种浏览器上正常工作的方式撕开样式或显示属性?

Thanks in Advance. 提前致谢。

Use a javascript library like jQuery that already has all the browser compatibility issues sorted and abstracted away. 使用像jQuery这样的javascript库,该库已经解决了所有浏览器兼容性问题,并将其抽象化。

From the docs it appears to support everything you need: 文档看来,它支持您需要的所有内容:

jQuery supports these browsers: jQuery支持以下浏览器:

* Firefox 2.0+
* Internet Explorer 6+
* Safari 3+
* Opera 9+
* Chrome 1+ 

As for the specific jQuery function to to this - look at .toggle() . 至于对此的特定jQuery函数-请查看.toggle()

...required to support IE6, 8, and Firefox. ...需要支持IE6、8和Firefox。 I've quickly discovered that none of these browsers contain a matching Javascript library. 我很快发现这些浏览器都不包含匹配的Javascript库。

jQuery , Prototype , and YUI all support those three browsers (and more). jQueryPrototypeYUI都支持这三种浏览器(及更多)。 I expect many of these others do as well. 我希望其他许多人也能做到。 Closure doesn't (at least, it doesn't claim to and we know Google dropped IE6 support in most of its products), but it's the first major library I know of to wave bye-bye to IE6. Closure并没有(至少,它并没有声称,并且我们知道Google在其大多数产品中都放弃了对IE6的支持),但这是我所知道的第一个向IE6告别的主要库。

Use a class instead. 改用一个类。 If you only use one class on your element, it's as simple as this: 如果您只在元素上使用一个类,则它很简单:

CSS: CSS:

*.hidden {
   display: none;
}

JavaScript: JavaScript:

function show(el) {
    el.className = "";
}

function hide(el) {
    el.className = "hidden";
}

show(document.getElementById("foo"));

you can still toggle between display:block/none; 您仍然可以在display:block / none之间切换; target the IEs via conditional comments or IE specific hacks to fix your layout issues. 通过条件注释或特定于IE的hack定位IE,以解决您的布局问题。 so lets say that you have width set on id uSub0 of 200px. 因此,假设您在ID uSub0上设置了200px的宽度。 and its breaking in IEs, you need it to be 190px for them. 及其在IE中的突破,您需要为它们设置190px。 heres how you can target them. 继承人如何定位他们。 example: 例:

uSub0{width:200px} uSub0 {width:200px}

/** ie6 only **/ / **仅ie6 ** /

uSub0{width:200px; uSub0 {width:200px; *width:190px} *宽度:190像素}

/** ie7 only **/ / **仅适用于IE7 ** /

uSub0{width:200px} uSub0 {width:200px}

:first-child+html #uSub0{width:190px} / * ie7 only **/ :first-child + html#uSub0 {width:190px} / *仅ie7 ** /

uSub0{width:200px} uSub0 {width:200px}

+html #uSub0{width:190px} / * ie6, ie7, ie8 **/ + html#uSub0 {width:190px} / * ie6,ie7,ie8 ** /

uSub0{width:200px; uSub0 {width:200px; width: 190px\\9} 宽度:190px \\ 9}

/** ie7, ie8 **/ / ** ie7,ie8 ** /

uSub0{width:200px; uSub0 {width:200px; width/ * /: 190px} 宽度/ * /:190像素}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM