2017-04-03 2 views
-5

자바 스크립트에서 일반 html을 인쇄하려고하는데 어떻게해야하는지 알 수 없습니다. 예 :Javascript : 일반 텍스트로 html 인쇄

$('#elem').html('This is a normal string'); // --> This is a normal string 

$('#elem').html('This <b>contains</b> html') 
//Current-> This contains html (contains is bold) 
//Wanted-> This <b>contains</b> html 

가 나는 사용자가 서면 의견과 함께 일하고 있기 때문에이 작업을 수행 할 수 있으며 자신의 의견의 일부를 넣어 경우 HTML 태그는 적용하지 않는다. 하지만 다른 이유로 태그를 볼 필요가 있습니다.

도움 주셔서 감사합니다.

+1

'.html' 대신'.text'를 사용하십시오! –

답변

-1

자동으로 HTML을 텍스트로 이스케이프 처리하는 jQuery .text() 함수를 사용하십시오!

이 예는 다음과 같습니다

$('#elem').text('This <b>contains</b> html') 
-2

당신은 jQuery를 .text() 방법을 사용할 수 있습니다.

$('#elem').text('This <b>contains</b> html'); 

예 :

<!DOCTYPE html> 
 
<html> 
 
<head> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script> 
 
<script> 
 
$(document).ready(function(){ 
 
    $("button").click(function(){ 
 
     $("#elem").text('This <b>contains</b> html'); 
 
    }); 
 
}); 
 
</script> 
 
</head> 
 
<body> 
 

 
<button>Click ME!!</button> 
 

 
<p id="elem">This is a paragraph.</p> 
 
    
 
</body> 
 
</html>

-1

다음은 작동합니다 :

$('#elem').text('This <b>contains</b> html'); 

관련 발췌문 :

행운

$('#an-id').text('<b>I don\'t want this to be HTML</b>');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<p id="an-id">This is not HTML!</p>

좋은!

자세한 내용은 jQuery documentation을 확인하십시오.

관련 문제