javascript - html代码不运行jquery问题

标签 javascript jquery

HTML 代码未运行!这里 item.m = "some html code",但它显示为纯文本,而不是运行 html 。我很困惑现在该怎么办..

$("#chatbox_"+chatboxtitle+" .chatboxcontent").append('<div class="chatboxmessage"><span class="chatboxmessagefrom">'+item.f+':&nbsp;&nbsp;</span><span class="chatboxmessagecontent">'+$.parseHTML(item.m)+'</span></div>');

完整代码:

$.ajax({
  url: "chat.php?action=chatheartbeat",
  cache: false,
  dataType: "json",
  success: function(data) {

    $.each(data.items, function(i,item){
        if (item)   { // fix strange ie bug

            chatboxtitle = item.f;

            if ($("#chatbox_"+chatboxtitle).length <= 0) {
                createChatBox(chatboxtitle);
            }
            if ($("#chatbox_"+chatboxtitle).css('display') == 'none') {
                $("#chatbox_"+chatboxtitle).css('display','block');
                restructureChatBoxes();
            }

            if (item.s == 1) {
                item.f = username;
            }

            if (item.s == 2) {
                $("#chatbox_"+chatboxtitle+" .chatboxcontent").append('<div class="chatboxmessage"><span class="chatboxinfo">'+item.m+'</span></div>');
            } else {
                newMessages[chatboxtitle] = true;
                newMessagesWin[chatboxtitle] = true;
                $("#chatbox_"+chatboxtitle+" .chatboxcontent").append('<div class="chatboxmessage"><span class="chatboxmessagefrom">'+item.f+':&nbsp;&nbsp;</span><span class="chatboxmessagecontent">'+$.parseHTML(item.m)+'</span></div>');
            }

            $("#chatbox_"+chatboxtitle+" .chatboxcontent").scrollTop($("#chatbox_"+chatboxtitle+" .chatboxcontent")[0].scrollHeight);
            itemsfound += 1;
        }
    });

响应数据为<a href='../chat/file/6717_1_work.txt'>work.txt</a>但它显示为文本而不是超链接。

最佳答案

JsFiddle

显然你需要删除 $.parseHTML 并且它会起作用

$("div").append('<span class="chatboxmessagecontent">'+("<a href='../chat/file/6717_1_work.txt'>work.txt</a>")+'</span></div>');

正如您在 jsFiddle 中的代码所看到的那样它不适用于 $.parseHTML

关于javascript - html代码不运行jquery问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28884453/

相关文章:

javascript - 回流: How to listen to a subset of actions using `listanables` or `this.listenToMany` ?

javascript - 带队列的Discord.js音乐机器人无法正常工作

javascript - 创建 JavaScript 测验应用程序(伪代码)

javascript - JSON 调用返回 [object Object] 或未定义

javascript - 在元素外部单击时如何调用函数并避免在单击事件内触发单击?

javascript - Backbone : Adding a model to a collection from a collection view?

javascript - 可以在没有第三方服务的情况下使用Apple Pay吗?

javascript - 单击后按钮保持焦点

javascript - 带缩略图和两个标题的图片库

php - Processmaker 中的 Jquery Ajaxing