innerText와 innerHTML의 차이점
차이점은 무엇이며 innerHTML
, innerText
그리고 childNodes[].value
자바 스크립트는?
달리 innerText
하지만, innerHTML
당신이 HTML 서식있는 텍스트로 작업 할 수 있습니다 자동으로 인코딩 및 디코딩 텍스트를하지 않습니다. 즉, innerText
태그의 내용을 일반 텍스트로 innerHTML
검색하고 설정하는 반면 HTML 형식으로 내용을 검색하고 설정합니다.
아래 예제는 다음 HTML 스 니펫을 나타냅니다.
<div id="test">
Warning: This element contains <code>code</code> and <strong>strong language</strong>.
</div>
노드는 다음 JavaScript로 참조됩니다.
var x = document.getElementById('test');
element.innerHTML
요소의 자손을 설명하는 HTML 구문을 설정하거나 가져옵니다.
x.innerHTML
// => "
// => Warning: This element contains <code>code</code> and <strong>strong language</strong>.
// => "
이것은 W3C의 DOM 구문 분석 및 직렬화 사양 의 일부입니다 . Element
개체 의 속성입니다 .
node.innerText
객체의 시작 태그와 종료 태그 사이에 텍스트를 설정하거나 가져옵니다.
x.innerText
// => "Warning: This element contains code and strong language."
innerText
Microsoft에 의해 소개되었으며 Firefox에 의해 지원되지 않았습니다. 2016 년 8 월 , WHATWGinnerText
에서 채택 되어 v45의 Firefox에 추가되었습니다.innerText
브라우저에서 렌더링 한 내용과 일치하는 텍스트를 스타일을 인식하고 표시하여 다음을 의미합니다.innerText
적용text-transform
및white-space
규칙innerText
줄 사이에 공백을 제거하고 항목 사이에 줄 바꿈을 추가합니다innerText
보이지 않는 항목에 대한 텍스트를 반환하지 않습니다
innerText
반환textContent
처럼 렌더링되지 않습니다 요소<style />
'와Node
요소의 속성
node.textContent
노드와 그 하위 항목의 텍스트 내용을 가져 오거나 설정합니다.
x.textContent
// => "
// => Warning: This element contains code and strong language.
// => "
이것은 W3C 표준 이지만 IE <9에서는 지원되지 않습니다.
- 스타일을 인식하지 못하므로 CSS로 숨겨진 컨텐츠를 리턴합니다.
- 리플 로우를 트리거하지 않습니다 (따라서 성능이 향상됨)
Node
요소의 속성
node.value
이것은 당신이 목표로 한 요소에 달려 있습니다. 위 예제의 경우 속성이 정의 되지 않은 HTMLDivElement 객체를 x
반환합니다 .value
x.value // => null
<input />
예를 들어 입력 태그 ( ) 는 "컨트롤의 현재 값"을 나타내는 속성을 정의합니다value
.
<input id="example-input" type="text" value="default" />
<script>
document.getElementById('example-input').value //=> "default"
// User changes input to "something"
document.getElementById('example-input').value //=> "something"
</script>
로부터 문서 :
참고 : 특정 입력 유형의 경우 반환 된 값이 사용자가 입력 한 값과 일치하지 않을 수 있습니다. 예를 들어, 사용자가 숫자가 아닌 값을에 입력
<input type="number">
하면 반환 된 값은 빈 문자열 일 수 있습니다.
샘플 스크립트
다음은 위에 제시된 HTML의 출력을 보여주는 예입니다.
var properties = ['innerHTML', 'innerText', 'textContent', 'value'];
// Writes to textarea#output and console
function log(obj) {
console.log(obj);
var currValue = document.getElementById('output').value;
document.getElementById('output').value = (currValue ? currValue + '\n' : '') + obj;
}
// Logs property as [propName]value[/propertyName]
function logProperty(obj, property) {
var value = obj[property];
log('[' + property + ']' + value + '[/' + property + ']');
}
// Main
log('=============== ' + properties.join(' ') + ' ===============');
for (var i = 0; i < properties.length; i++) {
logProperty(document.getElementById('test'), properties[i]);
}
<div id="test">
Warning: This element contains <code>code</code> and <strong>strong language</strong>.
</div>
<textarea id="output" rows="12" cols="80" style="font-family: monospace;"></textarea>
InnerText
속성 도는 내용을 HTML은-인코딩 <p>
에 <p>
당신은 당신이 사용할 필요가 HTML 태그를 삽입 할 경우 등 InnerHTML
.
간단히 말해서 :
innerText
값을 그대로 표시하고HTML
포함 할 수 있는 형식을 무시합니다 .innerHTML
값을 표시하고 모든HTML
형식을 적용합니다 .
var element = document.getElementById("main");
var values = element.childNodes[1].innerText;
alert('the value is:' + values);
예를 들어 더 세분화하고 Alec 값을 검색하려면 다른 .childNodes [1]를 사용하십시오.
var element = document.getElementById("main");
var values = element.childNodes[1].childNodes[1].innerText;
alert('the value is:' + values);
측면에서 MutationObservers
, 설정 innerHTML
생성 childList
의한 브라우저 노드를 제거한 다음의 값에 새로운 노드를 추가로 돌연변이 innerHTML
.
을 설정 innerText
하면 characterData
돌연변이가 생성됩니다.
InnerText
는 개행의 각 요소가 포함 된 페이지의 텍스트 값만 일반 텍스트로 innerHTML
반환하고 body
태그 내부의 모든 HTML 내용을 childNodes
반환하고 이름에서 알 수 있듯이 노드 목록을 반환합니다.
이 innerText
속성은 html 요소 의 실제 텍스트 값을 innerHTML
반환 하고을 반환합니다 HTML content
. 아래 예 :
var element = document.getElementById('hello');
element.innerText = '<strong> hello world </strong>';
console.log('The innerText property will not parse the html tags as html tags but as normal text:\n' + element.innerText);
console.log('The innerHTML element property will encode the html tags found inside the text of the element:\n' + element.innerHTML);
element.innerHTML = '<strong> hello world </strong>';
console.log('The <strong> tag we put above has been parsed using the innerHTML property so the .innerText will not show them \n ' + element.innerText);
console.log(element.innerHTML);
<p id="hello"> Hello world
</p>
참고 URL : https://stackoverflow.com/questions/19030742/difference-between-innertext-and-innerhtml
'Programing' 카테고리의 다른 글
iOS에서 base64 인코딩을 어떻게 수행합니까? (0) | 2020.04.17 |
---|---|
Visual Studio 2012 Ultimate RC에서 Intellisense 및 코드 제안이 작동하지 않음 (0) | 2020.04.17 |
C #에서 문자열을 여러 문자 구분 기호로 나누려면 어떻게합니까? (0) | 2020.04.17 |
미래와 약속의 차이점은 무엇입니까? (0) | 2020.04.17 |
왜 파이썬 코드를 컴파일해야합니까? (0) | 2020.04.17 |