當前位置:編程學習大全網 - 網站源碼 - 如何將用戶輸入的HTML源代碼輸出到網頁上

如何將用戶輸入的HTML源代碼輸出到網頁上

如果沒有後臺的話 直接用js轉換壹下尖括號就可以了

基本代碼如下:

<script?src="/jquery/3.2.1/jquery.js?"></script>

<textarea?id="htmls"></textarea>

<div?id="out"></div>

<span?id="clickBut">點擊</span>

<script?type="text/javascript">

$("#clickBut").click(function(){

var?html?=?$("#htmls").val();

html?=?html.replace(/</g,"&lt;");

html?=?html.replace(/</g,"&gt;");

html?=?html.replace(/\n/g,"<br>");

html?=?html.replace(/\t/g,"&nbsp;&nbsp;&nbsp;&nbsp;");

console.log(html);

$("#out").html(html);

})

</script>

我在mac chrome測試無問題。

具體的妳可以根據自己的需求再修改。

  • 上一篇:SAST面試什麽意思
  • 下一篇:問道數學題
  • copyright 2024編程學習大全網