【问题标题】:Jquery .load() removes highlight.js classesJquery .load() 删除 highlight.js 类
【发布时间】:2017-05-26 20:03:02
【问题描述】:

在我的 php 聊天中,每次提交消息时,我都会使用 .load() 来刷新我的 ajax 请求中的 div 内容。调用 .load 方法后,highlight.js 类消失。

Highlight.js 初始化

$(document).ready(function() {

hljs.configure({useBR: true});
$('div.sendermsg , div.receivermsg').each(function(i, block) 
  {
  hljs.highlightBlock(block);
  });

});

聊天 div

foreach($n as $x) { if ($x['username'] == $username) {?>

<tr>
<td class="sendertd">
<div class="sendermsg"><?php echo $x['message']; ?></div>
</td>
</tr>

<?php }

else { ?>

<tr>
<td class="receivertd">
<div class="receivermsg"><?php echo $x['message']; ?></div>
</td>
</tr>

<?php }
} ?>

阿贾克斯

$.ajax({
url : "backend/user_to_user.php", 
type : "POST", 
data : {'message':message,'user2':user2},
 success: function(data) 
  {
//syntax is highlighted and works well before this line is called
$(".tbody").load(location.href + " .tbody > *"); 
$(".tbody").scrollTop($(".tbody")[0].scrollHeight);

  }
   });

在 ajax 调用之前(如果发布的消息是 javascript,则只是一个示例)

ajax 调用后

有什么想法吗?

编辑 引用自 jquery.com

When calling .load() using a URL without a suffixed selector expression, the content is passed to .html() prior to scripts being removed. This executes the script blocks before they are discarded. If .load() is called with a selector expression appended to the URL, however, the scripts are stripped out prior to the DOM being updated, and thus are not executed.

我试过了

$(".tbody").load(location.href+" .tbody > *",function(){
$.getScript("js/highlight.min.js"); 
});

还是不行

【问题讨论】:

    标签: php jquery ajax highlight.js


    【解决方案1】:

    在.load() 之后添加它可以正常工作

    $(".tbody").load(location.href+" .tbody > *");
    $.getScript("http://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.11.0/highlight.min.js", function () {
    $('div.sendermsg , div.receivermsg').each(function(i, block) {
        hljs.highlightBlock(block);
    });
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-01
      • 2017-12-06
      相关资源
      最近更新 更多