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
1 Answer
Reset to default 8Newlines 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
评论列表(0条)