是否可以在某些浏览器中隐藏id元素

Is it possible to hide an id element in some browsers?

本文关键字:隐藏 id 元素 浏览器 是否      更新时间:2023-09-26

说我有一个像这个一样定义的id

<span id ="radiobuttonContainer"> Check to enable checkboxes: 
<input type="radio" name="cov-failed" value="cover" onclick="javascript:printoutCheckboxes('cover')">   
Coverage                
<input type="radio" name="cov-failed" value="failed" onclick="javascript:printoutCheckboxes('failed')"> 
Failed</span>

我不想在IE 9以下的浏览器中显示这个"spanId",因为这使IE<8无法管理。我知道,根据像这样的浏览器,可以有不同的javascript和css:es

<!--[if lte IE 8]><script language="javascript" type="text/javascript" src="root/include/excanvas.min.js"></script><![endif]--> 

然后选择显示:"无";

但我想知道,除了这一行之外,是否真的有必要拥有两个相等的.css文件,其中包含完全相同的信息?

我就不能做这样的事吗?

<!--[if lte IE 8]>document.getElementById('radiobuttonContainer').style.display = 'none';<![endif]-->

您应该能够将标记包装在条件注释中。或者,由于页面中内联包含的CSS优先于包含的CSS,因此不必包含第二个样式表,只需针对这种情况添加一个新的定义即可。即

<!--[if lte IE 8]>
<style type="text/css">
#radiobuttonContainer{
  display = 'none';
};
<![endif]-->

并将其放在包含的CSS文件之后。

如果您使用像Modernizr这样的工具,您可以使用它来确定浏览器的功能。

如果有一个特定的功能在IE8中不可用,您可以在样式表中引用它,如果浏览器没有该功能,则可以隐藏该元素。

假设这个功能是HTML5画布,你只需要一行脚本就可以在你的页面中包含Modernizr Javascript,然后你就可以在CSS中做这样的事情:

.nocanvas .radiobuttonContainer {
    display:none;
}

我认为你肯定可以做这样的事情:

<!--[if lte IE 8]><script type="text/javascript">
   document.getElementById('radiobuttonContainer').style.display = 'none';
</script><![endif]-->

顺便说一句,这是你的例子的合并。