zoukankan      html  css  js  c++  java
  • 写点js的小函数(一)

    写插件的博文实在是件伤神的事,太懒了,还是写点小函数吧。

    /*
     * $extendPrototype 扩展prototype函数
     * @param {Function} constructor
     * @param {Object} prototype
     
    */
    var $extendPrototype = function(constructor,prototype) {
        
    var c = constructor || function(){},
            p 
    = prototype || {};
        
    for(var atr in p) {
            c.prototype[atr] 
    = p[atr];
        }
        
    return c;
    }

    /* 
     * String扩展
     
    */
    var strEx = {
        removeJS : 
    function(){    /*删除Script字符串内容*/
            
    return this.replace(/<script[^>]*?>([\w\W]*?)<\/script>/ig,'');
        },
        getJS : 
    function(){        /*将Script字符串转换为Script对象,返回Script or false*/
            
    var js = this.replace(/[\s\S]*?<script[^>]*?>([\w\W]*?)<\/script>[\s\S]*?/g,'$1\r');
            
    if(js == this){
                
    return false;
            }
    else{
                
    var s = document.createElement('script');
                s.text 
    = js;
                
    return s;
            }
        },
        trim : 
    function(str){    /*删除首尾相应内容,参数为空则删除空格*/
            
    var reg;
            
    if(str){
                str 
    = str.replace(/([\.\+\?\*\\\^\&\[\]\(\)\{\}\$\,])/g,'\\$1');
                reg 
    = new RegExp("^(" + str +")+|(" + str + ")+$","g");/* 特殊字符 (. + ? * \ ^ & [ ] ( ) { } $ ,) */
            }
    else{
                reg 
    = /^\s+|\s+$/g;
            }
            
    return this.replace(reg,"");
        }
    }
    String 
    = $extendPrototype(String,strEx);

    innerHTML添加的js是不会执行的,所以要把js提取出来,使用<script>标签添加到html里就可以执行了。

    trim([str])删除首尾对应的内容,注意点就是一些特殊符号要记得处理

    转载请注明出处:http://www.cnblogs.com/lecaf/

    如有任何建议或疑问,欢迎留言讨论。

    如果觉得文章不错的话,欢迎点一下右下角的推荐。

  • 相关阅读:
    iframe应用 相互之间发送消息 postMessage
    function angular.bootstrap()
    总结
    1-angular.bind
    jQuery treeTable v 1.4.2
    声明了一个模块和一个控制器AngularJS的处理过程
    angularJs $templateCache
    $.fn.zTree 的使用
    Java异常throws与throw的区别
    Pom报错
  • 原文地址:https://www.cnblogs.com/lecaf/p/2068434.html
Copyright © 2011-2022 走看看