$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Javascript, trying to add linebreak inside create text node method - Stack Overflow|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Javascript, trying to add linebreak inside create text node method - Stack Overflow

matteradmin21PV0评论

This is my script:

<script>
/*jslint browser:true */
var i;
var out = document.getElementById("output");
var args = ["aaa", "bbb", "ccc", "ddd", 1, 2, 4 + 4];
function displayArgs() {
    "use strict";
    for (i = 0; i < args.length; i++) {
        out.appendChild(document.createTextNode(args[i] + "<br>"));
    }
}

displayArgs(args);
</script>

my output so far all appears on a single line with the break tags showing rather than b

This is my script:

<script>
/*jslint browser:true */
var i;
var out = document.getElementById("output");
var args = ["aaa", "bbb", "ccc", "ddd", 1, 2, 4 + 4];
function displayArgs() {
    "use strict";
    for (i = 0; i < args.length; i++) {
        out.appendChild(document.createTextNode(args[i] + "<br>"));
    }
}

displayArgs(args);
</script>

my output so far all appears on a single line with the break tags showing rather than b

Share Improve this question edited Oct 24, 2012 at 0:37 raina77ow 106k16 gold badges202 silver badges235 bronze badges asked Oct 24, 2012 at 0:36 jimeastjimeast 2771 gold badge2 silver badges13 bronze badges 0
Add a comment  | 

3 Answers 3

Reset to default 20

document.createTextNode prevents text from being rendered as html. Try this instead.

/*jslint browser:true */
var i;
var out = document.getElementById("output");
var args = ["aaa", "bbb", "ccc", "ddd", 1, 2, 4 + 4];
function displayArgs() {
    "use strict";
    for (i = 0; i < args.length; i++) {
        out.appendChild(document.createTextNode(args[i]));
        out.appendChild(document.createElement("br"));
    }
}
displayArgs(args);

Demo: http://jsfiddle.net/LVm9z/

You are creating a text node, so html tags are ignored. Use document.createElement instead:

var myDiv = document.createElement("div");
myDiv.id = 'myDiv';
myDiv.innerHTML = 'blah!<br/>';
document.body.appendChild(myDiv);

A text node is a text node, if it contains a <br> (or any HTML), it won't be parsed as HTML, as a text node's data is only treated as text.

You could add...

out.appendChild(document.createElement("br"));
Post a comment

comment list (0)

  1. No comments so far