js里现在存写模板的场景好多,如:弹框类的html代码模板等,js不支持换行的长文本写法,必需要一行行的加起来,如:代码如下:var content = 'divrow 1/div' 'divrow 2/div';而不可以写成:代码如下:var content = 'div row 1/divdivrow2 /div ';于是小加工一php小段代码,简化手工打的操作。

tojs.php 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 !DOCTYPE HTML PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN http://www.w3.org/TR/html4/loose.dtd html lang=en head meta http-equiv=Content-Type content=text/html;charset=UTF-8 titleStr To Js String/title style type=text/css .content-box { border: 1px #f0f0f0 slid; border-left: 4px #e0e0e0 solid; padding: 5px 5px 5px 10px; } /style /head body h1输入格式化的文本:/h1 ?php $jsContent = ''; if(isset($_POST['content']) $_POST['content']) { $content = strtr(htmlspecialchars($_POST['content']), array(rn = n)); $rows = explode(n, $content); foreach($rows as $row) { $jsContent .= ' '' . $row . 'br/; } $jsContent{0} = ' '; } ? form action=# method=post textarea name=content style=width: 99%;height: 300px; /textarea pinput type=submit value=提交 //p /form h2格式化后的结果:/h2 div class=content-box ?php echo $jsContent;? /div /body /html