jQuery截取字符串、日期字符串轉Date、獲取html中的純文本


jQuery截取字符串、日期字符串轉Date、獲取html中的純文本。

var com = com || {};
(function ($, com) {
    /*
    * 截取字符串
    * @param str:要截取的字符串
    * @param len:保留多少字符
    * @param symbol:超過之后字符串末端要添加的字符
    */
    com.cutStr = function (str, len, symbol) {
        if (symbol == undefined) {
            symbol = "...";
        }
        len = len || 25;
        var result = str;
        if (str) {
            if (str.length && str.length > len)
                result = str.substr(0, len) + symbol;
        }
        return result;
    },
    /*
    * 將日期字符串轉化為Date
    * (如:將"2016-12-24 20:13:14"轉化為Date格式)
    * @param d:待轉化字符串(傳入的時間不能有毫秒)
    */
    com.getDate = function (d) {
        //部分瀏覽器(IE)不支持日期格式“yyyy-MM-dd hh:mm:ss”,必須將“-”轉化為“/”
        var date = new Date(Date.parse(d.replace(/-/g, "/")));
        return date;
    },
    /*
    * 獲取html代碼的純文本
    * @param html
    */
    com.removeHTMLTag = function (html) {
        html = html.replace(/<\/?[^>]*>/g, ''); //去除HTML tag
        html = html.replace(/[ | ]*\n/g, '\n'); //去除行尾空白
        //html = html.replace(/\n[\s| | ]*\r/g,'\n'); //去除多余空行
        html = html.replace(/&nbsp;/ig, '');//去掉&nbsp;
        html = html.replace(/\s/g, ''); //將空格去掉
        return html;
    }
})(jQuery, com);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM