导航

Javascript 实现HTML字符串的储存

Posted on 2014-06-20 16:13  单只蝴蝶  阅读(232)  评论(0编辑  收藏  举报

     在js中使用HTML字符串时,可以不用理会字符串的单引号和双引号的转义,因为使用的注释,注释当然什么都可以写。

PS:这个有点类似于 php中的 <<<语法(heredoc和nowdoc),那么就为他命名heredoc吧。

Function.prototype.hereDoc = function(){
    var _str = this.toString(),
        _posS = _str.indexOf('/*') + 2 ;
        _posE = _str.lastIndexOf('*/') ;
        
    return (_posS < 0 || _posE < 0) ? '' : _str.substring(_posS, _posE) ;
}

function docWord(){
/*<table>
    <tr>
        <td>1</td>
        <td>2</td>
        <td>3</td>
        <td>4</td>
    </tr>
    <tr>
        <td>1</td>
        <td>2</td>
        <td>3</td>
        <td>4</td>
    </tr>
</table>*/
}

var table = docWord.hereDoc();
console.log(table);