JavaScript - 无效的参数 IE8

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6526713/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 20:57:10  来源:igfitidea点击:

JavaScript - invalid argument IE8

javascriptinternet-explorer-8

提问by Simon

I've got a little JavaScript problem. The code is working in Opera and Firefox browser but not in Internet Explorer 8. Does anybody know why?

我有一个 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";
    }
}

I'm just learning Javascript. Any feedback welcome. Simon

我只是在学习 Javascript。欢迎任何反馈。西蒙

回答by Mike Samuel

If object.widthis less than 16

如果object.width小于 16

hover.style.width = object.width - 16 + "px";

then this will produce a string with a negative sign at the front, which is illegal since widths have to be non-negative.

那么这将产生一个前面带有负号的字符串,这是非法的,因为宽度必须是非负的。

You can fix this by saying

你可以通过说来解决这个问题

hover.style.width = Math.max(object.width - 16, 0) + "px";

and similarly for height.

和高度类似。

Many browsers ignore invalid content, but IE in certain modes is stricter, so you are probably just getting silent failure in the others.

许多浏览器会忽略无效内容,但某些模式下的 IE 更严格,因此您可能只会在其他模式下静默失败。

回答by Saxoier

I guess it has to do with hover.setAttribute("class", "myarea");. If IE 8 is running in IE 7 or lower Mode this won't work. Then you have to use hover.className = 'myarea'(supported by all browsers).

我想这与hover.setAttribute("class", "myarea");. 如果 IE 8 在 IE 7 或更低模式下运行,这将不起作用。然后你必须使用hover.className = 'myarea'(所有浏览器都支持)。

The sAttrName parameter requires the name of the desired content attribute and not the Document Object Model (DOM) attribute. For example, in IE8 mode, this method no longer requires sAttrName to be "className" when setting, getting, or removing a CLASS attribute. Earlier versions of Internet Explorer and Internet Explorer 8 in compatibility mode still require sAttrName to specify the corresponding DOM property name.

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

http://msdn.microsoft.com/en-us/library/ms536739%28v=vs.85%29.aspx

Check the mode IE is running.

检查 IE 正在运行的模式。