2

在 Widget Factory 中对 jQuery 对象使用 $.extend 时,IE8 似乎丢失了新创建对象上的 jQuery 上下文。让我演示一下。

以下代码适用于 IE9+、Chrome、FireFox

$.widget("a07.Wooh", {
    options: {
        test: "Awesome"
    },
    _testFunc: function() {
        // Perform some operations on the DOM using this.element jQuery Object
        this.element.after("<div class=\"stuff\">Some cool stuff</div>").next().hide();
    },
    _testFunc2: function() {
        //Copy the this.element object
        this.element2 = $.extend({}, this.element);

        //Perform some operations on the DOM using this.element2 jQuery Object
        this.element2.next().css('color', 'red').show();
    },
    _create: function() {
        this._testFunc();
        this._testFunc2();
    },
    _init: function() {}
});

工作的jsfiddle

如上所述,此代码在IE8 之外的所有主要浏览器中都可以正常工作。基本上它返回该this.element2.next().css().show()行的错误消息:

对象不支持此属性或方法

它所指的属性/方法是 jQuery 方法 next()、css() 和 show()

看起来好像在 IE8 中 this.element2 已经失去了它的 jQuery 上下文,因为如果我像这样将对象包装在一个 jQuery 函数中:this.element2 = $(this.element2);一切都很好。

兼容 IE8 的 jsfiddle

所以问题是,这里发生了什么?这是 IE8 的标准行为还是我错误地以编程方式处理这种情况?

4

1 回答 1

1

如果您的意图只是创建一个包含相同元素的单独 jQuery 对象,那么如何:

this.element2 = $( this.element[0] );
于 2012-11-12T00:27:34.670 回答