JavaScript代码在IE8中不起作用
下面的代码在IE8中不起作用,但是,它在FireFox和Google Chorome上工作得非常好,即使IE8没有抛出错误,但是,没有输出。有什么想法吗?实际问题是什么JavaScript代码在IE8中不起作用,javascript,internet-explorer-8,Javascript,Internet Explorer 8,下面的代码在IE8中不起作用,但是,它在FireFox和Google Chorome上工作得非常好,即使IE8没有抛出错误,但是,没有输出。有什么想法吗?实际问题是什么 <html> <head/> <body> <script type="text/javascript"> (function(){ var inpEle = document.createElement("div");
<html>
<head/>
<body>
<script type="text/javascript">
(function(){
var inpEle = document.createElement("div");
inpEle.setAttribute("id", "div1");
var texEle = document.createTextNode("This is my Sample Para. I am testing it again my own level that prove How i am capable of.");
inpEle.appendChild(texEle);
document.body.appendChild(inpEle);
})();
(function(){
var inpEle1 = document.createElement("input");
inpEle1.setAttribute("type", "button");inpEle1.setAttribute("value", "Show");inpEle1.setAttribute("onclick", "Show()");
document.body.appendChild(inpEle1);
var inpEle2 = document.createElement("input");
inpEle2.setAttribute("type", "button");inpEle2.setAttribute("value", "Hide");inpEle2.setAttribute("onclick", "Hide()");
document.body.appendChild(inpEle2);
})();
</script>
<script type="text/javascript">
window.onload= function(){
document.getElementById('div1').style.display="none";
}
Show = function (){
document.getElementById('div1').style.border="2pt solid green";
document.getElementById('div1').style.display="";
}
Hide = function(){
document.getElementById('div1').style.border="";
document.getElementById('div1').style.display="none";
}
</script>
</body>
</html>
(功能(){
var inpEle=document.createElement(“div”);
输入setAttribute(“id”,“div1”);
var texEle=document.createTextNode(“这是我的示例段落。我正在以我自己的水平再次测试它,以证明我的能力。”);
输入附加子项(texEle);
document.body.appendChild(inpEle);
})();
(功能(){
var inpEle1=document.createElement(“输入”);
inpEle1.setAttribute(“type”、“button”);inpEle1.setAttribute(“value”、“Show”);inpEle1.setAttribute(“onclick”、“Show()”;
文件.正文.附件(第1栏);
var inpEle2=document.createElement(“输入”);
inpEle2.setAttribute(“type”、“button”);inpEle2.setAttribute(“value”、“Hide”);inpEle2.setAttribute(“onclick”、“Hide()”;
文件.正文.附件(inpEle2);
})();
window.onload=function(){
document.getElementById('div1').style.display=“无”;
}
Show=函数(){
document.getElementById('div1').style.border=“2pt纯绿色”;
document.getElementById('div1').style.display=“”;
}
Hide=函数(){
document.getElementById('div1').style.border=“”;
document.getElementById('div1').style.display=“无”;
}
转到Internet选项-安全选项卡-Internet-单击“自定义”按钮,然后向下滚动到杂项部分。“允许脚本启动的窗口不受大小或位置限制”找到“活动脚本”,然后选中“启用”。您可能无法使用document.body.appendChild()在IE8中,
标记已被解析,并且您的代码仍在被解析时尝试附加到正文之前。当你这么做的时候,早期版本的IE比如IE6可能只是中止(例如,字面上的崩溃)。更高版本(如IE8)只会忽略您的请求
可以使用document.write()
在解析正文时添加内容
您可以推迟调用代码,直到主体完成加载和解析(如
处理程序)或触发事件(如window.onload
)后再调用
您可以appendChild()
添加到已完成解析的元素(脚本之前的内容)
最简单的解决方案可能是将
放在脚本之前的正文中,并附加到正文而不是正文中,或者将代码放在函数中,让正文onload事件调用函数
有关此问题的说明,请参阅。不要使用setattribute对事件使用匿名函数
inpEle1.onclick = function() {
Show();
};
它也可以在IE8的IE tester中使用,但不能在IE7中使用。您是否使用IE7兼容功能?我注意到element.style.display并不总是与IE8兼容
以下是一种提高兼容性的方法:
if (element.style.setAttribute)
element.style.setAttribute("display", value);
else
element.style.display = value;
致以最诚挚的问候,我建议您阅读。现在,代码很难阅读。如果你独自一人在编写代码,你可以按自己的意愿格式化它。但是如果你希望其他人阅读你的代码,你应该努力使它尽可能可读。你现在缩进了你的HTML,但没有在代码中使用缩进来提高它的可读性。这与问题有什么关系?你是在暗示IE8中没有启用脚本吗?解释一下为什么这与这个问题相关是合适的。你说它在FireFox和Google Chorome上运行得很好。因此,IE8可能会有问题。可能是脚本没有启用。如果是,你必须启用它