简体   繁体   English

如何禁用div中的HTML标签?

[英]How to disable HTML tags in a div?

I have a page to show a log file, sometimes the log file contains some html tags and they make my page run slower since the tags are converted to contents like pictures etc., I want to disable that, so my goal is to show those tags as texts only, they should never be converted to contents. 我有一个页面显示一个日志文件,有时该日志文件包含一些html标记,并且由于标记被转换为图片等内容,它们使我的页面运行变慢,我想禁用该标记,所以我的目标是显示那些标签仅作为文本使用,切勿将其转换为内容。

This is the that I use: 这是我使用的

<script type="text/javascript">
  $(document).ready(function() {
    setInterval(function() {
      $.ajax({
        url: "ssh.log",
        success: function(result) {
          $("#ssh").html(result);
        }
      });
      var textarea = document.getElementById("ssh");
      textarea.scrollTop = textarea.scrollHeight;
    }, 1000);
  });
</script>

And my div looks like this: 我的div看起来像这样:

<div id="ssh"></div>

How can I accomplish that? 我该怎么做?

使用.text()将内容呈现为文本:

$("#ssh").text(result);

Change: 更改:

$("#ssh").html(result);

to

$("#ssh").text(result);

As the previous answers have said, use .text() to render it as just plaintext. 如先前的回答所述,使用.text()将其呈现为纯文本格式。 If for some reason you need to keep it as .html() , you can use simple string replacement to escape the tags. 如果出于某种原因需要将其保留为.html() ,则可以使用简单的字符串替换来逸出标签。

result = result.replace('<','&lt;');
$('#ssh').html(result);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM