본 사이트는 여러분의 사용자 경험을 개선하기 위해 유지 보수를 진행하는 동안 기능이 제한됩니다. 도움말로 문제가 해결되지 않고 질문을 하고 싶다면 Twitter의 @FirefoxSupport 및 Reddit의 /r/firefox 채널을 활용하세요.

Mozilla 도움말 검색

고객 지원 사기를 피하세요. 저희는 여러분께 절대로 전화를 걸거나 문자를 보내거나 개인 정보를 공유하도록 요청하지 않습니다. "악용 사례 신고"옵션을 사용하여 의심스러운 활동을 신고해 주세요.

자세히 살펴보기

In a DIV with contenteditable, adding a space at the end of a word causes a BR tag to be added

  • 2 답장
  • 3 이 문제를 만남
  • 7 보기
  • 최종 답변자: Florin

more options

Hi, In a DIV element with contenteditable, I have only 1 word which is bold, italic and underlined. This is the HTML content of the DIV element (divObj.innerHTML):

<u><em><strong>TEST</strong></em></u>

If I put the caret at the end of the word and hit SPACE, 1 space is inserted (visible); but the innerHTML is this:

<u><em><strong>TEST <br></strong></em></u>

so there's a space, but also a BR tag. Why is there a BR tag added when only SPACE was pressed?

sample test.html:

<script>
function showhtml() {
	alert(document.getElementById("mydiv").innerHTML);
}
</script>
<div id="mydiv" style="width:500px; height:100px; border:1px solid blue;" contenteditable><u><em><strong>TEST</strong></em></u></div>
<input type="button" value="show html" onclick="showhtml()">
Hi, In a DIV element with contenteditable, I have only 1 word which is bold, italic and underlined. This is the HTML content of the DIV element (divObj.innerHTML): &lt;u&gt;&lt;em&gt;&lt;strong&gt;TEST&lt;/strong&gt;&lt;/em&gt;&lt;/u&gt; If I put the caret at the end of the word and hit SPACE, 1 space is inserted (visible); but the innerHTML is this: &lt;u&gt;&lt;em&gt;&lt;strong&gt;TEST&nbsp;&lt;br&gt;&lt;/strong&gt;&lt;/em&gt;&lt;/u&gt; so there's a space, but also a BR tag. Why is there a BR tag added when only SPACE was pressed? sample test.html: <pre> &lt;script> function showhtml() { alert(document.getElementById("mydiv").innerHTML); } &lt;/script&gt; &lt;div id="mydiv" style="width:500px; height:100px; border:1px solid blue;" contenteditable&gt;&lt;u&gt;&lt;em&gt;&lt;strong&gt;TEST&lt;/strong&gt;&lt;/em&gt;&lt;/u&gt;&lt;/div&gt; &lt;input type="button" value="show html" onclick="showhtml()"&gt; </pre>

글쓴이 cor-el 수정일시

모든 댓글 (2)

more options

That <br> tag may already have been there by default and wasn't removed when you entered that text.

more options

Have you tried the test code I ? There's no BR initially in the DIV. The initial DIV content is exactly the one mentioned in the question: so only U, EM and STRONG tags around the word TEST. No BRs. And you can verify that with the test code I posted in the question. Initially there's no BR tag, but after I add a SPACE at the end of the word, a BR tag is also added. Also, if I hit ENTER 1 time, I get 2 BR tags instead of 1.

It also happens if the DIV's initial content is just TEST (the tags U, EM and STRONG are not actually needed to reproduce this problem). And if you delete all text, there's still a BR tag returned.

Anyway, I noticed that the bogus BR tag has the attribute _moz_dirty. So it's probably used internally by Firefox's DOM engine. But even so, it shouldn't be returned by the innerHTML property of the DIV node, since it's not actually part of the content.

글쓴이 Florin 수정일시