我正在尝试找出如何将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>
您的代码对我来说很好。 IE11有时会阻止脚本,如果您要打开网站,则应该在浏览器底部看到一个弹出窗口。您还可以确保将控制台日志添加到方法中。
这可能是与您缓存相关的问题。尝试按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浏览器中的输出:
如果问题仍然存在,请尝试使用我发布的代码进行测试,以查看其是否有效。