翻译来自prototype de jquery的插入新元素

时间:2015-07-06 06:00:45

标签: javascript jquery prototype

我正在尝试翻译以下原型函数:

function addCommentDump(message, type, info) {
    var commentDump = $('comment_dump');
    if (!commentDump) {
        var container = $('comment_dump_container');
        if (!container) {
            return;
        }
        container.insert(commentDump = new Element('div', {
            'id': 'comment_dump'
        }));
    }
    if (info) {
        message += ': ' + info;
    }
    commentDump.insert(new Element('span', {
        'class': type
    }).update(message));
}

到一个jquery。这是代码:

function addCommentDump(message, type, info) {
    //alert("working.....");
    var $commentDump = $('#comment_dump');
    if (!$commentDump) {
        var $container = $('#comment_dump_container');
        if (!$container) {
            return;
        }
        $container.append($commentDump = new Element('div', {
            'id': 'comment_dump'
        }));
    }
    if (info) {
        message += ': ' + info;
    }
    $commentDump.append(new Element('span', {
        'class': type
    }).html(message));
}

我收到错误:TypeError:非法构造函数。 在此先感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

dismiss()不是Element中的类。你无法致电jQuery。 要创建新元素,您可以使用new Element

更改此

$('<div />')

$container.append($commentDump = new Element('div', {
    'id': 'comment_dump'
}));

$container.append($('<div />').attr({
    'id': 'comment_dump'
}));

$commentDump.append(new Element('span', {
    'class': type
}).html(message));
相关问题