Javascript, trying to add linebreak inside create text node method

前端 未结 3 1918
执念已碎
执念已碎 2021-02-08 06:34

This is my script:



        
相关标签:
3条回答
  • 2021-02-08 07:03

    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);
    
    0 讨论(0)
  • 2021-02-08 07:21

    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", "ffffd", 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/

    0 讨论(0)
  • 2021-02-08 07:28

    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"));
    
    0 讨论(0)
提交回复
热议问题