常用js函数Common.js

 


/*------------------------------------------------------------
  Trim()去左右空格
  ------------------------------------------------------------*/

String.prototype.Trim = function() {
    return this.replace(/(^\s*)|(\s*$)/g, "");
}
String.prototype.LTrim = function() {
    return this.replace(/(^\s*)/g, "");
}
String.prototype.Rtrim = function() {
    return this.replace(/(\s*$)/g, "");
}


/*------------------------------------------------------------
  截取字符串,中文2个字符,英文1个字符
  ------------------------------------------------------------*/
function subString(str, len, hasDot) {
var newLength = 0;
var newStr = "";
var chineseRegex = /[^\x00-\xff]/g;
var singleChar = "";
var strLength = str.replace(chineseRegex, "**").length;
for (var i = 0; i < strLength; i++) {
singleChar = str.charAt(i).toString();
if (singleChar.match(chineseRegex) != null) {
newLength += 2;
}
else {
newLength++;
}
if (newLength > len) {
break;
}
newStr += singleChar;
}

if (hasDot && strLength > len) {
newStr += "...";
}
return newStr;
}

/*------------------------------------------------------------
  replaceAll全部替换
  ------------------------------------------------------------*/
String.prototype.replaceAll = function(s1, s2) {
    return this.replace(new RegExp(s1, "gm"), s2);
}

/*------------------------------------------------------------
  不区分大小写的 javascript indexOf
  兼容原来的 indexOf
  ------------------------------------------------------------*/

String.prototype._indexOf = String.prototype.indexOf;
String.prototype.indexOf = function() {
    if (typeof (arguments[arguments.length - 1]) != 'boolean')
        return this._indexOf.apply(this, arguments);
    else {
        var bi = arguments[arguments.length - 1];
        var thisObj = this;
        var idx = 0;
        if (typeof (arguments[arguments.length - 2]) == 'number') {
            idx = arguments[arguments.length - 2];
            thisObj = this.substr(idx);
        }

        var re = new RegExp(arguments[0], bi ? 'i' : '');
        var r = thisObj.match(re);
        return r == null ? -1 : r.index + idx;
    }
}
/*------------------------------------------------------------
  过滤HTML标记
  ------------------------------------------------------------*/
function fliterHTML(sTest) {
var reTag = /<(?:.|\s)*?>/g;
var a = sTest.replace(reTag, "");
return a;
}
/*------------------------------------------------------------
  兼容FF的previousSibling
  ------------------------------------------------------------*/
function previousElementSibling(lipre) {
try {
do {

lipre = lipre.previousSibling;
if (!lipre) return null;
} while (lipre.nodeType != 1)//nodeType = 3是文本节点 nodeType = 1是html节点
return lipre;
}
catch (ex) { return null; }
}

/*------------------------------------------------------------
  兼容FF的nextSibling
  ------------------------------------------------------------*/
function nextElementSlibing(linext) {
    try {
        do {

            linext = linext.nextSibling;
            if (!linext) return null;

        } while (linext.nodeType != 1)//nodeType = 3是文本节点 nodeType = 1是html节点
        return linext;
    }
    catch (ex) { return null; }
}


/*------------------------------------------------------------
  获取当前浏览器
------------------------------------------------------------*/
function myBrowser() {
    var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
    var isOpera = userAgent.indexOf("Opera") > -1;


    if (isOpera) { return "Opera" }; //判断是否Opera浏览器
    if (userAgent.indexOf("Firefox") > -1) { return "FF"; } //判断是否Firefox浏览器
    if (userAgent.indexOf("Safari") > -1) { return "Safari"; } //判断是否Safari浏览器
    if (userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 && !isOpera) { return "IE"; }; //判断是否IE浏览器
}


/*------------------------------------------------------------
  兼容firefox的 outerHTML 使用以下代码后,firefox可以使用element.outerHTML 
  ------------------------------------------------------------*/
if (myBrowser()=="FF") {
    HTMLElement.prototype.__defineSetter__("outerHTML", function(sHTML) {
        var r = this.ownerDocument.createRange();
        r.setStartBefore(this);
        var df = r.createContextualFragment(sHTML);
        this.parentNode.replaceChild(df, this);
        return sHTML;
    });
    HTMLElement.prototype.__defineGetter__("outerHTML", function() {
        var attr;
        var attrs = this.attributes;
        var str = "<" + this.tagName.toLowerCase();
        for (var i = 0; i < attrs.length; i++) {
            attr = attrs[i];
            if (attr.specified)
                str += " " + attr.name + '="' + attr.value + '"';
        }
        if (!this.canHaveChildren)
            return str + ">";
        return str + ">" + this.innerHTML + "</" + this.tagName.toLowerCase() + ">";
    });


    HTMLElement.prototype.__defineGetter__("canHaveChildren", function() {
        switch (this.tagName.toLowerCase()) {
            case "area":
            case "base":
            case "basefont":
            case "col":
            case "frame":
            case "hr":
            case "img":
            case "br":
            case "input":
            case "isindex":
            case "link":
            case "meta":
            case "param":
                return false;
        }
        return true;
    });


 转载请注明出处:http://blog.csdn.net/dasihg/article/details/6800413

原文地址:https://www.cnblogs.com/dashi/p/4034771.html