【发布时间】: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