myutil.js

//---------为String增加的方法---------- //字符串去空格 function String.prototype.trim() {return this.replace(/(^/s*)|(/s*$)/g,"");} function String.prototype.trimAll(){return this.replace(//s/g, "");} //去掉所有空格 function String.prototype.Ltrim(){return this.replace(/(^/s*)/g, "");} function String.prototype.Rtrim(){return this.replace(/(/s*$)/g, "");} //字符串转换成数字 function String.prototype.toFloat(){ if(this.trim()==""||isNaN(this)){ return 0; }else{ return parseFloat(this); } } //将xml字符串解析成xml对象 function String.prototype.toXML(){ try{ var xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); //var xmlDoc = new ActiveXObject("Msxml2.DOMDocument"); xmlDoc.loadXML(this); }catch(e){ var oParser=new DOMParser(); xmlDoc=oParser.parseFromString(this,"text/xml"); } return xmlDoc; } //---------表单方法----------------- //为form增加一个hidden域 function createHidden(form,name,value){ var e= document.createElement("input"); e.setAttribute("type","hidden"); e.setAttribute("name",name); e.setAttribute("value",value); form.insertBefore(e); } //只显示strArray数组中有的select选项 function showSelect(selectName,strArray){ var opts = selectName; for(var i=0; i<opts.length; i++){ var boo = 0; for(var j = 0; j<strArray.length; j++){ if(strArray[j]==opts[i].value){ boo=1; } } if(boo==0){ var child = opts.children(i); opts.removeChild(child); i--; } } } //获取焦点后,让光标在文本末尾 function setCaretAtEnd(field) { if(field.createTextRange) { var r = field.createTextRange(); r.moveStart('character', field.value.length); r.collapse(); r.select(); } } //将页面所有text框设置为只读 function setAllTextReadOnly(){ var obj = document.getElementsByTagName("input"); for(var i=0;i<obj.length;i++){ if(obj[i].type=="text"){ obj[i].readOnly = true; } } } //-----------自定义对象方法--------- function myclass(){}; myclass.prototype = { value:"", setValue:function(v){ this.value = v; }, print:function(){ alert(this.value); }, write:function(){ document.write("<select name=a>"); document.write("<option value="+this.value+">"+this.value+"</option>"); document.write("</select>"); } } var my = new myclass(); my.setValue("hahahaha"); my.print(); my.write(); //--------------------------------------

你可能感兴趣的:(xml,function,String,input,character)