0

我的代码实际上在 chrome 中运行良好,但在 IE 中测试失败:/ e.element().hide() 和 e.element().insert() 不起作用。Ajax.Request() 在两种浏览器中都可以正常工作。知道如何解决吗?

$$('a.deleteButton').invoke('observe', 'click', function(e) {
            var commentID = parseInt(this.readAttribute('data-commentid'));
            e.stop();
            new Ajax.Request('index.php?action=CNewsComment',
                    {
                        method: 'POST',
                        parameters: {entryID: commentID, cNewsAction: 'delete'},
                        onSuccess: function(transport){
                            new Effect.toggle('commentID' + commentID, 'Appear', {duration:0.5});
                            var count = parseInt($('commentsCount').innerHTML.stripTags());
                            $('commentsCount').innerHTML = count-1;
                        },
                        onLoading: function(transport){
                            e.element().hide(); //not working in IE but in Chrome

                            if ($('deleteLoading' + commentID)) {
                                $('deleteLoading' + commentID).show();
                            }
                            else {
                                e.element().up().insert ({ 
                                    'before'  : '<img id="deleteLoading' + commentID + '" src="' + RELATIVE_WCF_DIR + 'images/cNewsSpinnerS.gif" alt="" />'
                                });
                            }
                        },
                        onFailure: function(transport){ 
                            e.element().show();                         
                            $('deleteLoading' + commentID).hide();                          
                            alert("An error occurred: " + transport.status + ' - ' + transport.statusText);
                        }
            });
    });

更新:如果我将在 IE 中不起作用的代码放在 AJAx-Request 之外,它正在工作:

$$('a.deleteButton').invoke('observe', 'click', function(event) {
            event.stop();
            // IE PART START
            event.element().hide();
            if ($('deleteLoading' + commentID)) {
                $('deleteLoading' + commentID).show();
            }
            else {
                event.element().up().insert ({ 
                'before'  : '<img id="deleteLoading' + commentID + '" src="' + RELATIVE_WCF_DIR + 'images/cNewsSpinnerS.gif" alt="" />'
                });
            }
            // IE PART END
            new Ajax.Request('index.php?action=CNewsComment',
                    {
                        method: 'POST',
                        parameters: {entryID: commentID, cNewsAction: 'delete'},
                        onLoading: function(transport){
                        }

但是在 onLoading-function 里面却不是,为什么?!

4

3 回答 3

0
$$('a.deleteButton').invoke('observe', 'click', function(e) {
        var commentID = parseInt(this.readAttribute('data-commentid'));
        e.stop();
        new Ajax.Request('index.php?action=CNewsComment',
                {
                    method: 'POST',
                    parameters: {entryID: commentID, cNewsAction: 'delete'},
                    onSuccess: function(transport){
                        new Effect.toggle('commentID' + commentID, 'Appear', {duration:0.5});
                        var count = parseInt($('commentsCount').innerHTML.stripTags());
                        $('commentsCount').innerHTML = count-1;
                    },
                    onLoading: (function(transport, e){
                        e.element().hide(); //not working in IE but in Chrome

                        if ($('deleteLoading' + commentID)) {
                            $('deleteLoading' + commentID).show();
                        }
                        else {
                            e.element().up().insert ({ 
                                'before'  : '<img id="deleteLoading' + commentID + '" src="' + RELATIVE_WCF_DIR + 'images/cNewsSpinnerS.gif" alt="" />'
                            });
                        }
                    }).bindAsEventListener(this),
                    onFailure: function(transport){ 
                        e.element().show();                         
                        $('deleteLoading' + commentID).hide();                          
                        alert("An error occurred: " + transport.status + ' - ' + transport.statusText);
                    }
        });
});
于 2012-10-29T04:24:15.140 回答
0

尝试这个:

    $$('a.deleteButton').invoke('observe', 'click', function(e) {
        var element = e.element();
        var commentID = parseInt(this.readAttribute('data-commentid'));
        e.stop();
        new Ajax.Request('index.php?action=CNewsComment', {
            method: 'POST',
            parameters: {entryID: commentID, cNewsAction: 'delete'},
            onSuccess: function(transport) {
                new Effect.toggle('commentID' + commentID, 'Appear', {duration:0.5});
                var count = parseInt($('commentsCount').innerHTML.stripTags());
                $('commentsCount').innerHTML = count-1;
            },
            onLoading: function(transport) {
                element.hide(); // not working in IE but in Chrome

                var delId = 'deleteLoading' + commentID;
                if ($(delId)) {
                    $(delId).show();
                } else {
                    element.up().insert({ 
                        'before': '<img id="' + delId + '" src="' + RELATIVE_WCF_DIR + 'images/cNewsSpinnerS.gif" alt="" />'
                    });
                }
            },
            onFailure: function(transport) { 
                element.show();                         
                $('deleteLoading' + commentID).hide();                          
                alert("An error occurred: " + transport.status + ' - ' + transport.statusText);
            }
        });
    });

如果此代码不起作用,请尝试onLoading用此变体替换:

            onLoading: function(element, transport) {
                element.hide(); // not working in IE but in Chrome

                var delId = 'deleteLoading' + commentID;
                if ($(delId)) {
                    $(delId).show();
                } else {
                    element.up().insert({ 
                        'before': '<img id="' + delId + '" src="' + RELATIVE_WCF_DIR + 'images/cNewsSpinnerS.gif" alt="" />'
                    });
                }
            }.curry(element),

PS你使用哪个IE版本?较旧的 IE 具有单个全局事件对象window.event,并且在异步回调时,您的eeven in 关闭可能不会指向您的原始点击事件,而是指向在onLoading调用之前触发的另一个事件。

于 2012-11-05T18:05:41.447 回答
-1

我只是对原型了解一点。但是您如何调试以检查“e”是否是您要使用的元素。有时我会遇到这种问题。

于 2012-10-28T04:43:36.867 回答