jquery .text不会将HTML元素呈现到DOM中

时间:2009-11-19 15:20:31

标签: ajax jquery

我正在使用以下一点jQuery将图像列表加载到div中:

                var jewellerDdl = "<%= JewellerDropDownList.ClientID %>";
                var filter = $("#" + jewellerDdl).val();

                $.ajax({
                    type: "POST",
                    url: "popup.aspx/GetJewellerAssets",
                    contentType: "application/json; charset=utf-8",
                    data: '{"jewellerId":' + filter + '}',
                    dataType: "json",
                    success: AjaxSucceeded,
                    error: AjaxFailed
                });

                function AjaxSucceeded(result) {
                    $("#divEntryDisplay").text(result.d);
                }

                function AjaxFailed(result) {
                    alert(result.status + ' - ' + result.statusText);
                } 

我收到的字符串是<ul>,其中包含一些图片

但是,.text方法不会将它们添加到dom中。相反,它只是将HTML作为文本打印在名为divEntryDisplay

<div>

我做错了什么?

3 个答案:

答案 0 :(得分:5)

你确实应该使用html方法(参见文档:http://docs.jquery.com/Attributes/html

这使你的功能看起来像这样:

 function AjaxSucceeded(result) {
    $("#divEntryDisplay").html(result.d);
 }

答案 1 :(得分:2)

请改为尝试:

$("#divEntryDisplay").html(result.d);

答案 2 :(得分:1)

如果您将HTML代码插入DOM,则必须使用html()方法,而不是text()方法。