1

$("<div/>").text(value).html is not a function在下面的代码中收到错误:

function htmlEncode(value) { 
    return $('<div/>').text(value).html(); 
}

function startImageUpload(imageuploadform, imagefilename){

    $('.imagef1_cancel').eq(window.lastUploadImageIndex).html(
        '<div>' + 
        htmlEncode(imagefilename) + 
        '<button type="button" class="imageCancel" cancel_image_file_name="' + 
        imagefilename + 
        '">CANCEL</button></div>'
    );

    return true;
}

如何修复此错误?

奇怪的是我在下面有一个类似的代码,但功能不同(stopImageUpload)并且代码在下面工作正常:

 function htmlEncode(value) { 
        return $('<div/>').text(value).html(); 
    }

    function stopImageUpload(success, imagefilename) {
        imagecounter++;
        $('.listImage').eq(window.lastUploadImageIndex).append(
            '<div>' + 
            htmlEncode(imagefilename) +
            '<button type="button" class="deletefileimage" image_file_name="' +
            imagefilename + 
            '">Remove</button><br/><hr/></div>'
        ); 

        return true;   
    }

链接到应用程序以查看正在发生的事情在这里:应用程序

4

2 回答 2

3
$('<div/>').html(value); //without text function
于 2012-05-15T12:40:22.710 回答
1
function htmlEncode(value) { 
    return $('<div/>').text(value).html(); 
}

如果value在上面的代码片段中是undefined,则text()返回 DOM 元素的文本内容,而不是元素本身,导致链式调用失败。

于 2012-05-15T12:36:58.837 回答