如何在没有浏览器渲染的情况下发布HTML代码 [英] How to post HTML code without the browser rendering

查看:144
本文介绍了如何在没有浏览器渲染的情况下发布HTML代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I'm currently working on my school project and am to build a forum for the project,But right now am facing this problem where i want users to be able to post their html source code as it works in this forum,but the problem is that the code runs or scatters my design when retrieve from my DB.I try using repalce() in jquery but i could only replace < with < but i want a function to be able to replace others such as >,",' and & so my question is how can i write this function.







the below code work to replace the < but when i try including >,",' and & in the function it will stop work how can i make it work please someone should help me out.





我试过的:





What I have tried:

function convert(div){
         var str =  $(div).html();
         var str2 =  str.replace(/</g,"&lt;");
           var sta =  $(div).html(str2);
             return sta;
            }

推荐答案

(div).html();
var str2 = str.replace(/< / g,& lt;);
var sta =
(div).html(); var str2 = str.replace(/</g,"&lt;"); var sta =


(div).html(str2);
返回sta;
}
(div).html(str2); return sta; }


Quote:

the下面的代码工作,以取代<但当我尝试包括>,,和&在功能中它将停止工作我怎么能让它工作,请有人应该帮助我。

the below code work to replace the < but when i try including >,",' and & in the function it will stop work how can i make it work please someone should help me out.



我认为关键首先是以任何顺序替换& first然后替换其他字符。


I think the key is to start with replacing & first and then the other chars in any order.

var str2 =  str.replace(/&/g,"&amp;");
var str2 =  str2.replace(/</g,"&lt;");

var str2 =  str2.replace(/>/g,"&gt;");
...



因为所有编码的字符都以'&'开头,所以你需要先替换它。


Because all encoded chars starts with '&', you need to replace it first.


这篇关于如何在没有浏览器渲染的情况下发布HTML代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆