码迷,mamicode.com
首页 > 编程语言 > 详细

javascript 字符串常用的自定义函数,加载css etc.

时间:2018-06-11 18:58:36      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:date   sele   before   class   document   ret   cti   insert   sel   

// 去字符串#后面的部分  http://localhost:8090/admin/activity.html#list => list
String.prototype.hash = function() {
    var index = this.lastIndexOf("#");
    return index < 0 ? "" : this.substring(index);
};

// 字符串宽度 汉字算1个 英文字母数字算半个
String.prototype.width = function() {
    var c = 0, inc;
    for (var i = 0, n = this.length; i < n; i++) {
        inc = this.charCodeAt(i) < 256 ? 0.5 : 1;
        c += inc;
    }
    return c;
}

String.prototype.mb_substr = function(w) {
    var c = 0, inc, i;
    for (i = 0, n = this.length; i < n && c <= w; i++) {
        inc = this.charCodeAt(i) < 256 ? 0.5 : 1;
        c += inc;
    }
    return this.substring(0, i);
}

// 返回整数数组的最大值
Array.prototype.max = function() {
    var max = this[0];
    for (var i = 1, n = this.length; i < n; i++) {
        if (this[i] > max) {
            max = this[i];
        }
    }
    return max;
};

// 去掉html中的标签  <div><p>...
function strip_tags(html) {
    var div = document.createElement("div");
    div.innerHTML = html;
    return (div.textContent || div.innerText || "");
}

// 去的当前年月 201806
function get_yyyymm() {
    var date = new Date();
    var month = date.getMonth()+1;
    return (date.getYear() + 1900) + ""+ (month<10 ? "0"+month : month);
}

// 引入css(jquery-ui.css), 到head >link:first-child前
// load css before all <link> tags
function css(href) {
    var link = document.createElement("link");
    link.type = "text/css";
    link.rel = "stylesheet";
    link.href = href;
    var dom = document.head.querySelector("link");
    document.head.insertBefore(link, dom);
}

  

javascript 字符串常用的自定义函数,加载css etc.

标签:date   sele   before   class   document   ret   cti   insert   sel   

原文地址:https://www.cnblogs.com/mingzhanghui/p/9168316.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!