js里現(xiàn)在存寫模板的場景好多,如:彈框類的html代碼模板等,js不支持換行的長文本寫法,必需要一行行的加起來,如:
代碼如下:
var content = '<div>row 1</div>'
+ '<div>row 2</div>';
而不可以寫成:
代碼如下:
var content = '<div> row 1</div>
<div>row2 </div> ';
于是小加工一php小段代碼,簡化手工打的操作。
tojs.php
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>Str 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("\r\n" => "\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>
<p><input type="submit" value="提交" /></p>
</form>
<h2>格式化后的結(jié)果:</h2>
<div class="content-box">
<?php echo $jsContent;?>
</div>
</body>
</html>
更多信息請查看IT技術(shù)專欄