常用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/archive/2011/09/22/4034771.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值