常用js

来源:互联网 发布:木工数控机床编程 编辑:程序博客网 时间:2024/06/04 00:25
//正则检验function check_reg(reg,str) {     if(reg.test(str)){        return true;    }    else {        return false;    }}//去除前后空格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,'')};//js 的String有去空格的方法a.trim()a.trimLeft()a.trimRight()//获取XMLHttpRequest 对象function xmlhttpRequest(){    var xmlhttp;    if (window.XMLHttpRequest) {        xmlhttp = new XMLHttpRequest();    }    else {        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");    }    return xmlhttp}//ajax封装函数,xmlhttp:XMLHttpRequest 对象,func:处理响应的函数,url:请求的地址,method:请求方式,param:请求参数function myajax(xmlhttp,func,url,method="GET",param="") {    xmlhttp.onreadystatechange = func;    xmlhttp.open(method,url,true);    if (method == "POST"){        xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");    }    xmlhttp.send(param);}