//这样虽换行,但在好多<p>里输出 window.onload = function(){ var arrText = ["one","two","three","four","five","six","seven","eight","nine","ten"]; var oFragment = document.createDocumentFragment(); for(var i = 0;i<arrText.length;i++){ var oP = document.createElement("p"); var oText = document.createTextNode(arrText[i] ); oP.appendChild(oText); oFragment.appendChild(oP); } document.body.appendChild(oFragment); }; //这样在一个<p>里输出了,但不换行,咋整? window.onload = function(){ var arrText = ["one","two","three","four","five","six","seven","eight","nine","ten"]; var oFragment = document.createDocumentFragment(); var oP = document.createElement("p"); for(var i = 0;i<arrText.length;i++){ var oText = document.createTextNode(arrText[i] ); oP.appendChild(oText); oFragment.appendChild(oP); } document.body.appendChild(oFragment); };
加 “var oText = document.createTextNode(arrText[i] + "\n");”
只是加了个空格
先输入换行符:\n,然后设置css属性white-space: pre;
//文档碎片 window.onload = function(){ var arrText = ["one","two","three","four","five","six","seven","eight","nine","ten"]; var oFragment = document.createDocumentFragment(); var oP = document.createElement("p"); for(var i = 0,len = arrText.length;i<len;i++){ var oText = document.createTextNode(arrText[i]); oText[i] +"\n"; oText[i].style.whiteSpace = "pre"; oP.appendChild(oText); oFragment.appendChild(oP); } document.body.appendChild(oFragment) ; };
这些什么都没输出啊,不会了。
@Coca-code: ...不是这样用的。在控制台执行如下代码:
document.write('abc\ndef');
document.body.style.whiteSpace='pre';
即可看到效果。
@幻天芒:
var arrText = ["one","two","three","four","five","six","seven","eight","nine","ten"]; var oFragment = document.createDocumentFragment(); var oP = document.createElement("p"); for(var i = 0,len = arrText.length;i<len;i++){ var oText = document.createTextNode(arrText[i] + '\n'); oP.appendChild(oText); oFragment.appendChild(oP); } document.body.appendChild(oFragment) ; document.body.style.whiteSpace='pre'; };
最终输出顶部怎么没空行?
@Coca-code: 这个你自己F12看具体的元素了。
@幻天芒: document.body.style.whiteSpace='pre';
oP.style.whiteSpace='pre';
加错对象了,要加深css,要调试
@Coca-code: :)
@幻天芒: 问下,对node,meteor有研究麽,都和js紧密相关,
@Coca-code: node知道,Meteor是指https://github.com/meteor/meteor么?
@幻天芒: 是咯,资料很多~
http://www.maiziedu.com/course/python/342-3449/
这个里面涉及调用本地,多端同步,数据库等,一个不错的技术。以后也会在这方面下工,但js得搞上去...
你的<br/>也是一个Element吧。。。
是个html标签,每次循环,打一个<br/>也不好,
还能换行吗?
自己找个折中的办法吧