使用JavaSCript在IE中更改ID css

问题描述 投票:0回答:2

我正在尝试找出如何将CSS显示从无更改为使用JavaScript阻止,但仅适用于IE。目前,我有此代码,但它不会更改ID#backfill-image上的CSS。

<script>
function isIE() {
  ua = navigator.userAgent;
  /* MSIE used to detect old browsers and Trident used to newer ones*/
  var is_ie = ua.indexOf("MSIE ") > -1 || ua.indexOf("Trident/") > -1;

  return is_ie; 
}
/* Create an alert to show if the browser is IE or not */
if (isIE()){

document.getElementById("backfill-image").style.display = "block";
}
</script>
javascript html css internet-explorer display
2个回答
0
投票

您的代码对我来说很好。 IE11有时会阻止脚本,如果您要打开网站,则应该在浏览器底部看到一个弹出窗口。您还可以确保将控制台日志添加到方法中。


0
投票

这可能是与您缓存相关的问题。尝试按CTRL + F5键硬刷新页面。

您的代码正站在我这一边。

<!DOCTYPE html> 
<html> 
<head>
<style>
#backfill-image
{
	display:none;
}
</style>

</head>
<body> 
	<div id="backfill-image"><h2>This is my sample text...</h2></div>

<script>
function isIE() {
  ua = navigator.userAgent;
  /* MSIE used to detect old browsers and Trident used to newer ones*/
  var is_ie = ua.indexOf("MSIE ") > -1 || ua.indexOf("Trident/") > -1;

  return is_ie; 
}
/* Create an alert to show if the browser is IE or not */
if (isIE()){

document.getElementById("backfill-image").style.display = "block";
}
</script>
</body> 
</html>

在IE 11浏览器中的输出:

enter image description here

如果问题仍然存在,请尝试使用我发布的代码进行测试,以查看其是否有效。

© www.soinside.com 2019 - 2024. All rights reserved.