我正在尝试使用JavaScript从DBase打印出文本字段,在该字段中,PHP将Textfield提供给具有ID的Textarea。打印JavaScript函数使用getElementById从Textarea中获取内容并打印它。但这是问题。要正确显示在Textarea中,Textfield中有新线。这些在Textarea中是看不见的。他们只是出现在线路休息时。(它们在DBASE表中使用PHP管理员也是看不见的。它们也出现在那儿时出现)。
。但是,要在打印文本时,要使行破坏出现,需要将它们转换为<br>
。除了,您会看到并打印一个Runown句子。
我认为我需要在JavaScript变量中搜索某些内容,也许是 n并替换为
,但是,我似乎无法在打印页面中显示linebreaks(WICH是基于HTML的。)
任何人都可以建议正确的方法吗?谢谢。
有缺陷的代码:JSf
unction printit(id) {
var toprint = document.getElementById(id).innerHTML;
toprint = toprint.replace("n","<br>");
win = window.open();
self.focus();
win.document.open();
win.document.write('<'+'html'+'><'+'head'+'><'+'style'+'>');
win.document.write('body, td { font-family: Verdana; font-size: 10pt;}');
win.document.write('<'+'/'+'style'+'><'+'/'+'head'+'><'+'body'+'>');
win.document.write(toprint);
win.document.write('<'+'/'+'body'+'><'+'/'+'html'+'>');
win.document.close();
win.print();
win.close();
}
html
<textarea id="textarea">A whole bunch
of text
with line breaks
goes here
</textarea><a href="javascript:void(0);" onclick="printit('textarea');">Print text</a>
您可能只替换第一行断路。尝试全局标志代替它们:
toprint = toprint.replace(/n/g, '<br />');
我相信您需要将其准确打印出文本区域中的方式。Textarea将线路包裹应用于其内容。这些不是断线字符,因此不能被替换。
将内容附加到 wextarea 之前的元素上。
。即。
...
win.document.write('<'+'/'+'style'+'><'+'/'+'head'+'><'+'body'+'>');
// element with same width as textarea
win.document.write('<div style="width: '+ document.getElementById(id).offsetWidth +'px">');
win.document.write(toprint);
win.document.write('</div>');
win.document.write('<'+'/'+'body'+'><'+'/'+'html'+'>');
...
在HTML中显示新线字符的最佳解决方案是使用CSS White-space 属性。