Jquery .load()删除highlight.js类

时间:2017-05-26 10:28:03

标签: php jquery ajax highlight.js

在我的php聊天中,每次提交消息时,我都会使用*.java刷新我的ajax请求中的div内容。在调用.load方法之后,突出显示.js类dissapear。

Highlight.js init

.load()

聊天divs

$(document).ready(function() {

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

});

的Ajax

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调用之前

(如果发布的消息是javascript,只是一个例子)

before call

在ajax调用之后

after call

有什么想法吗?

EDIT 引自jquery.com

$.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); } });

所以我试过

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.

仍然无法正常工作

1 个答案:

答案 0 :(得分:0)

通过在.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);
});
});