admin管理员组文章数量:1303450
I'm trying to add a line break using inner HTML. My code so far is as follows:
document.getElementById("demo").innerHTML += "hello";
document.getElementById("demo").innerHTML += linebreakhere;
I've been searching, and is there a way to add like a <b> like the above?
I'm trying to add a line break using inner HTML. My code so far is as follows:
document.getElementById("demo").innerHTML += "hello";
document.getElementById("demo").innerHTML += linebreakhere;
I've been searching, and is there a way to add like a <b> like the above?
Share Improve this question edited Feb 6, 2023 at 2:58 Peter Mortensen 31.6k22 gold badges110 silver badges133 bronze badges asked Jun 16, 2016 at 2:49 ollielouisollielouis 91 gold badge1 silver badge3 bronze badges 3- 1 Either pass <br/> in string or add CSS block property to element so that element goes to next line – Shivani Commented Jun 16, 2016 at 2:53
-
1
That code is inefficient. Re-querying the DOM for getting the same element and modifying
innerHTML
, the worst way of appending elements. – Ram Commented Jun 16, 2016 at 2:57 -
Re "
<b>
": Do you mean "<br>
"? – Peter Mortensen Commented Feb 6, 2023 at 3:00
2 Answers
Reset to default 7You do this:
document.getElementById("demo").innerHTML += "hello";
document.getElementById("demo").innerHTML += "<br>";
A few ways you can achieve the line break using innerHtml
Example 1:
document.getElementById("demo").innerHTML = "Hello <br/> World";
<div id="demo"></div>
Example 2:
document.getElementById("demo").innerHTML = "<pre>" + "Hello" + "\n" + "World"+ "</pre>";
<div id="demo"></div>
本文标签: innerhtmlHow do I add a line break to a 39div39 using inner HTML in JavaScriptStack Overflow
版权声明:本文标题:innerhtml - How do I add a line break to a 'div' using inner HTML in JavaScript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741751481a2395857.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论