그래서 본문, 단일 div 및 텍스트가있는 단락 요소가 포함 된 매우 기본적인 페이지가 있다고 가정 해 보겠습니다.텍스트 노드의 "nodeValue"속성이 비어 있습니다. 어떻게 테스트합니까?
<body>
<div>
<p>some text</p>
<div>
</body>
은 브라우저에 따라, 신체/DIV 요소는 텍스트 노드 (nodeType에의 === 3, nodeValue를 === "--blank--")을해야합니다. 그러나 P 엘레멘트는 nodeValue === "some text"를 가진 정당한 텍스트 노드를 가질 것이다.
궁금한 점은 공백을 나타내는 '가짜'텍스트 노드의 "nodeValue"(--blank--)가 무엇인지 궁금합니다. 필자가 테스트를 작성하여 필터를 제거하도록할까요? 가짜 텍스트 노드.
if(body.childNodes[0].nodeValue.trim()) {
// more than white space
} else {
// just white space
}
공백 만 있었다면 당신은거야 :
예 : 왼쪽 거기에 아무것도 경우
var body = document.getElementsByTagName("body")[0]; // shortcut to body element.
console.log(body.childNodes[0].nodeValue) // maps to one of the fake text nodes that are blank.
// returns a blank line. What is that "blank" equal to? It's not 'null' or "" or undefined...
건배, 알렉스
본문에 액세스하는 가장 빠른 방법은 * document *의 본문 * 속성 (예 : [document.body] (http://www.w3.org/TR/DOM-Level-2-HTML/))입니다. html.html # ID-56360201). – RobG