html - Javascript - document.createTextNode() - Stack Overflow

Using this strips my newLine charactersIs there an alternative to this that will render html?function v

Using this strips my newLine characters

Is there an alternative to this that will render html?

 function viewCommentToggle( ment )
{
    theRow = document.getElementById("id"+ment.id);

    idx = 2;


        // Comment field
        cell = theRow.cells[idx];
        while( cell.childNodes.length > 0 ) cell.removeChild(cell.childNodes[0]);

            element = document.createTextNode(mentment);

        cell.appendChild(element);


         }

This is what Im concered with:

element = document.createTextNode(mentment);

just a fyi....this is what I did and it worked:

    function viewCommentToggle( ment )
{
    theRow = document.getElementById("id"+ment.id);

    idx = 2;


        // Comment field
        //cell = theRow.cells[idx];
        // while( cell.childNodes.length > 0 ) cell.removeChild(cell.childNodes[0]);

        cell = $("#id"+ment.id+" > td:eq("+idx+")");
        $(cell).empty();

        $(cell).html( mentment == null ? "" : mentment.replace(/\n/g,"<br/>").replace(/\r/g,"") ); 

Using this strips my newLine characters

Is there an alternative to this that will render html?

 function viewCommentToggle( ment )
{
    theRow = document.getElementById("id"+ment.id);

    idx = 2;


        // Comment field
        cell = theRow.cells[idx];
        while( cell.childNodes.length > 0 ) cell.removeChild(cell.childNodes[0]);

            element = document.createTextNode(ment.ment);

        cell.appendChild(element);


         }

This is what Im concered with:

element = document.createTextNode(ment.ment);

just a fyi....this is what I did and it worked:

    function viewCommentToggle( ment )
{
    theRow = document.getElementById("id"+ment.id);

    idx = 2;


        // Comment field
        //cell = theRow.cells[idx];
        // while( cell.childNodes.length > 0 ) cell.removeChild(cell.childNodes[0]);

        cell = $("#id"+ment.id+" > td:eq("+idx+")");
        $(cell).empty();

        $(cell).html( ment.ment == null ? "" : ment.ment.replace(/\n/g,"<br/>").replace(/\r/g,"") ); 
Share Improve this question edited Apr 18, 2012 at 20:43 Doc Holiday asked Apr 18, 2012 at 15:35 Doc HolidayDoc Holiday 10.3k32 gold badges102 silver badges153 bronze badges 1
  • yup, using jQuery does make it all a lot easier... – Alnitak Commented Apr 18, 2012 at 20:52
Add a ment  | 

1 Answer 1

Reset to default 8

Newlines are only significant (AFAIK) within a <pre> block.

Outside of that, to force line breaks you'll have to split your string into separate lines and then create a text node followed by a <br/> for each one, i.e. something like:

var lines = text.split('\n');
var parent = document.body; // the node you want to insert the string into
for (var i = 0; i < lines.length; ++i) {
    parent.appendChild(document.createTextNode(lines[i]));
    parent.appendChild(document.createElement('br'));
}

See http://jsfiddle/alnitak/WFTD6/

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744356144a4570240.html

相关推荐

  • html - Javascript - document.createTextNode() - Stack Overflow

    Using this strips my newLine charactersIs there an alternative to this that will render html?function v

    7天前
    70

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信