IT

텍스트 영역 내에서 HTML 렌더링

lottoking 2020. 6. 29. 07:37
반응형

텍스트 영역 내에서 HTML 렌더링


텍스트 영역 (예 : <strong>, <i>, <u>, <a>) 내에 일부 HTML 태그를 렌더링 할 수 있어야하지만 텍스트 영역은 내용을 텍스트로만 해석합니다. 외부 라이브러리 / 플러그인 (jQuery를 사용하고 있음)에 의존하지 않고 쉽게 수행 할 수 있습니까? 그렇지 않은 경우이 작업을 수행하는 데 사용할 수있는 jQuery 플러그인을 알고 있습니까?


이 작업은 할 수 없습니다 textarea. 당신이 찾고 있는 것은 매우 쉽게 수행 할 수 있는 내용 편집 가능한 div입니다.

<div contenteditable="true"></div>

jsFiddle

div.editable {
    width: 300px;
    height: 200px;
    border: 1px solid #ccc;
    padding: 5px;
}

strong {
  font-weight: bold;
}
<div contenteditable="true">This is the first line.<br>
See, how the text fits here, also if<br>there is a <strong>linebreak</strong> at the end?
<br>It works nicely.
<br>
<br><span style="color: lightgreen">Great</span>.
</div>


편집 가능한 div를 사용하면 메소드 document.execCommand( 자세한 내용 )를 사용하여 지정한 태그 및 기타 기능을 쉽게 지원할 수 있습니다.

#text {
    width : 500px;
	min-height : 100px;
	border : 2px solid;
}
<div id="text" contenteditable="true"></div>
<button onclick="document.execCommand('bold');">toggle bold</button>
<button onclick="document.execCommand('italic');">toggle italic</button>
<button onclick="document.execCommand('underline');">toggle underline</button>


렌더 만했기 때문에 가능합니다. 이 라인을 따라 뭔가를 할 수 있습니다.

function render(){
	var inp     = document.getElementById("box");
	var data = `
<svg xmlns="http://www.w3.org/2000/svg" width="${inp.offsetWidth}" height="${inp.offsetHeight}">
<foreignObject width="100%" height="100%">
<div xmlns="http://www.w3.org/1999/xhtml" 
style="font-family:monospace;font-style: normal; font-variant: normal; font-size:13.3px;padding:2px;;">
${inp.value} <i style="color:red">cant touch this</i>
</div>
</foreignObject>
</svg>`;
	var blob = new Blob( [data], {type:'image/svg+xml'} );
	var url=URL.createObjectURL(blob);
	inp.style.backgroundImage="url("+URL.createObjectURL(blob)+")";
 }
 onload=function(){
  render();
  ro = new ResizeObserver(render);
	ro.observe(document.getElementById("box"));
 }
#box{
  color:transparent;
  caret-color: black;
  font-style: normal;/*must be same as in the svg for caret to align*/
  font-variant: normal; 
  font-size:13.3px;
  padding:2px;
  font-family:monospace;
}
<textarea id="box" oninput="render()">you can edit me!</textarea>

이것은 textareaHTML을 렌더링 할 수 있도록합니다 ! 크기를 조정할 때 깜박임, 클래스를 직접 사용할 수 없으며 div의 div 가 캐럿이 올바르게 정렬되는 svg형식과 동일한 형식 인지 확인해야 textarea합니다.


이것에 대한 부록. 당신은 (예 : 변경과 같은 문자 엔터티 사용할 수 있습니다 <div>로를 &lt;div&gt;) 그것은 텍스트 영역에 렌더링됩니다. 그러나 저장 될 때 텍스트 영역의 값은 렌더링 된 텍스트 입니다. 따라서 인코딩을 해제 할 필요가 없습니다. 방금 브라우저에서 이것을 테스트했습니다 (즉, 11로 돌아 가기).


JSFiddle 에서이 예제를 사용해보십시오

<style>
.editable {
    width: 300px;
    height: 200px;
    border: 1px solid #ccc;
    padding: 5px;
    resize: both;
    overflow: auto;
}
</style>

<div class="editable" contenteditable="true"></div>
<button class="bold">toggle red</button>
<button class="italic">toggle italic</button>

<script>
function toggleRed() {
    var text = $('.editable').text();
    $('.editable').html('<p style="color:red">'+text+'</p>');
}

function toggleItalic() {
    var text = $('.editable').text();
    $('.editable').html("<i>"+text+"</i>");
}

$('.bold').click(function () {
    toggleRed();
});

$('.italic').click(function () {
    toggleItalic();
});
</script>

Jeffrey Crowder에 의해 편집-웹 개발자 / Windows 프로그래머

코드의 JS 바이올린을 추가했습니다.


I have the same problem but in reverse, and the following solution. I want to put html from a div in a textarea (so I can edit some reactions on my website; I want to have the textarea in the same location.)

To put the content of this div in a textarea I use:

var content = $('#msg500').text();
$('#msg500').wrapInner('<textarea>' + content + '</textarea>');
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="msg500">here some <strong>html</strong> <i>tags</i>.</div>


This is possible with <textarea> the only thing you need to do is use Summernote WYSIWYG editor

it interprets HTML tags inside a textarea (namely <strong>, <i>, <u>, <a>)

참고URL : https://stackoverflow.com/questions/4705848/rendering-html-inside-textarea

반응형