admin管理员组文章数量:1402917
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/
本文标签: htmlJavascriptdocumentcreateTextNode()Stack Overflow
版权声明:本文标题:html - Javascript - document.createTextNode() - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744356144a2602314.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论