JavaScript實現(xiàn)存儲HTML字符串示例
更新時間:2014年04月21日 17:26:20 作者:
這篇文章主要介紹了JavaScript存儲HTML字符串的具體實現(xiàn),需要的朋友可以參考下
我是搞PHP的,讓我想起了<<<語法(heredoc和nowdoc),那么就為他命名heredoc吧。
Function.prototype.heredoc = function(){
// 利用 function 的注釋來存儲字符串,而且無需轉(zhuǎn)義。
var _str = this.toString(),
s_pos = _str.indexOf("/*")+2,
e_pos = _str.lastIndexOf("*/");
return (s_pos<0 || e_pos<0) ? "" : _str.substring(s_pos, e_pos);
}
function fn(){
/*<table>
<tr>
<td>用戶名</td>
<td>密碼</td>
</tr>
<tr>
<td style="widht:20px;">@name</td>
<td>zf123456</td>
</tr>
</table>*/
}
var str_table = fn.heredoc();
console.log(str_table);
復(fù)制代碼 代碼如下:
Function.prototype.heredoc = function(){
// 利用 function 的注釋來存儲字符串,而且無需轉(zhuǎn)義。
var _str = this.toString(),
s_pos = _str.indexOf("/*")+2,
e_pos = _str.lastIndexOf("*/");
return (s_pos<0 || e_pos<0) ? "" : _str.substring(s_pos, e_pos);
}
function fn(){
/*<table>
<tr>
<td>用戶名</td>
<td>密碼</td>
</tr>
<tr>
<td style="widht:20px;">@name</td>
<td>zf123456</td>
</tr>
</table>*/
}
var str_table = fn.heredoc();
console.log(str_table);
相關(guān)文章
js實現(xiàn)點擊切換checkbox背景圖片的簡單實例
下面小編就為大家?guī)硪黄猨s實現(xiàn)點擊切換checkbox背景圖片的簡單實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05
javascript實現(xiàn)Emrips反質(zhì)數(shù)枚舉的示例代碼
下面小編就為大家分享一篇javascript實現(xiàn)Emrips反質(zhì)數(shù)枚舉的示例代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-12-12
用JavaScript實現(xiàn)用一個DIV來包裝文本元素節(jié)點
當我試圖將文本(可能也包含HTML元素)用一個DIV元素包起來時,可以使用下面的方法,需要的朋友可以參考下2014-09-09

