写点小函数!
innerHTML添加的js是不会执行的,所以要把js提取出来,使用 script 标签添加到html里就可以执行了。
trim([str])删除首尾对应的内容,注意点就是一些特殊符号要记得处理
/* * $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); |