【问题标题】:In IE7 and IE6, setting element.style['display'] = 'inherit' thows an exception在 IE7 和 IE 6 中,设置 element.style['display'] = 'inherit' 会引发异常
【发布时间】:2010-08-22 16:23:23
【问题描述】:

对于这个 IE 错误有什么好的解决方法,它仍然允许我使用 javascript 在 IE7 或 IE6 中将 HTML 元素的显示样式设置为“继承”?

    var el = document.getElementById('someElementID');
    if (!el) return false;
    try {
        var displayValue = 'inherit';
        //the next line throws exception in IE7 and IE6
        // when displayValue = 'inherit'
        // but not for other legit values like 'none', 'block', 'inline' or even ''
        el.style['display'] = displayValue;
    } catch (e) {
        alert(e.message);
    }

假设我想将visible: hiddenposition:absolute 设置为不可见或将z-index 设置为其他内容。

【问题讨论】:

  • display: inherit?你需要这个做什么?
  • display 在某种意义上已经被“继承”了:如果你在一个元素上设置display: none,所有的子元素都被隐式隐藏了。
  • ...这意味着如果您希望display 属性被继承,请执行以下操作:el.style.display = '';

标签: javascript css internet-explorer


【解决方案1】:

display 属性的许多值是在 CSS 2 中添加的,inherit 就是其中之一。较旧的浏览器不支持这些新值。您可以比较CSS 1CSS 2 中的可能值。

即使您可以设置该值,它也无用,因为浏览器不知道如何处理它。

【讨论】:

  • 我不介意浏览器忽略它,就像抛出异常一样。事实证明,在所有允许的 CSS2 显示属性中,inherit 是唯一在 IE6&7 中引发异常的属性。好的,谢谢大家的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多