Sim*_*mon 9 javascript internet-explorer-8
我有一点JavaScript问题.代码在Opera和Firefox浏览器中工作,但在Internet Explorer 8中没有.有人知道为什么吗?
function createbtn(object, inner) {
var hover = document.createElement("div");
hover.setAttribute("class", "myarea");
if (inner) {
hover.style.width = object.width - 16 + "px";
hover.style.height = object.height - 16 + "px";
hover.style.top = getposy(object) + "px";
hover.style.left = getposx(object) + "px";
} else {
hover.style.width = object.width + "px";
hover.style.height = object.height + "px";
hover.style.top = getposy(object) - 8 + "px";
hover.style.left = getposx(object) - 8 + "px";
}
}
Run Code Online (Sandbox Code Playgroud)
我只是在学习Javascript.任何反馈欢迎.西蒙
如果object.width小于16
hover.style.width = object.width - 16 + "px";
Run Code Online (Sandbox Code Playgroud)
那么这会在前面产生一个带负号的字符串,这是非法的,因为宽度必须是非负的.
你可以通过说来解决这个问题
hover.style.width = Math.max(object.width - 16, 0) + "px";
Run Code Online (Sandbox Code Playgroud)
和高度相似.
许多浏览器会忽略无效内容,但某些模式下的IE更严格,因此您可能只是在其他浏览器中遇到静默故障.
我想这与hover.setAttribute("class", "myarea");. 如果 IE 8 在 IE 7 或更低模式下运行,则此操作将不起作用。那你就得用hover.className = 'myarea'(所有浏览器都支持)。
sAttrName 参数需要所需内容属性的名称,而不是文档对象模型 (DOM) 属性的名称。例如,在 IE8 模式下,当设置、获取或删除 CLASS 属性时,此方法不再要求 sAttrName 为“className”。早期版本的 Internet Explorer 和 Internet Explorer 8 在兼容模式下仍然需要 sAttrName 来指定相应的 DOM 属性名称。
http://msdn.microsoft.com/en-us/library/ms536739%28v=vs.85%29.aspx
检查 IE 正在运行的模式。