javascript错误:匿名函数IE中的参数无效

时间:2011-07-30 18:55:15

标签: javascript internet-explorer

我在Internet Explorer中遇到了一个奇怪的错误(我试过IE 8) 使用不同的浏览器,一切都很好。

与此类似的函数的参数无效(我无法发布原始函数,因为它来自最初被混淆和压缩的不可再发行的库):

function createADiv() {
    var f = document.createElement('div');
    f.set = [function (z) { // error on this line
        f.style.width = z
    }, function (z) {
        f.style.height = z
    }];
    return f;
 }

问题在于f的范围,但我不明白为什么在匿名函数中使用f应该不起作用。

有关如何规避此Internet Explorer错误的任何想法?

我已经检查了其他“无效参数错误”,但这个似乎是另一种情况。

提前致谢

2 个答案:

答案 0 :(得分:0)

我认为这就是你要做的事情:

function createADiv() {
    var f = document.createElement('div');
    f.set = {
        width: function (z) { // error on this line
            f.style.width = z
        }, 
        height: function (z) {
            f.style.height = z
        }
    };
    return f;
 }

现在当你创建一个div时,你可以这样做:

var newDiv = createADiv();
newDiv.set.height("200px");

但是说实话这没用,因为javascript已经这样做了:

newDiv.style.height = "200px";

没有太大区别

答案 1 :(得分:0)

不确定原因,请尝试将其重新编写为

function createADiv() {
    var f = document.createElement('div');
    f.set = [function a (z) { // error on this line
        f.style.width = z
    }, function b (z) {
        f.style.height = z
    }];
    return f;
 }