[英]Line break inside text area html
您需要將文本區域中的換行符( \r
或\n
)轉換為 PDF 可以理解的換行符( <br />
<div>
<li>
等)。
一個簡單的 PHP 方式是
$string = nl2br($string);
// converts \n to <br />
如果您需要即時轉換這些換行符(例如您正在捕獲輸入並將其顯示為用戶類型的格式),請在 javascript 中進行。 這是一個方便的 function 取自: https://stackoverflow.com/a/7467863/1772933
function nl2br (str, is_xhtml) {
if (typeof str === 'undefined' || str === null) {
return '';
}
var breakTag = (is_xhtml || typeof is_xhtml === 'undefined') ? '<br />' : '<br>';
return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1' + breakTag + '$2');
}
這是用戶鍵入時如何使用 function 的示例
$('#TextArea').keypress(function(evt){
$('#TextArea').html(nl2br($('#TextArea').val())); // replace linebreaks first
$('#pdfPreviewArea').html($('#TextArea').val()); // copy to #pdfPreviewArea
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.