/*---访问文档元素-----------------------------------------*/功能元素(id){return document.getElementById(id);}/*---JavaScript技巧----------------------------------------------------*///能够使用将“this”称为回调的对象方法//见第3.3节https://github.com/spencertipping/js-in-ten-minutes/raw/master/js-in-ten-minutes.pdf函数绑定(f,thisvalue){返回函数(){return f.apply(thisvalue,arguments)};};//为缺少Array.isArray的旧浏览器实现Array.isArray。// https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/isArrayif(!Array.isArray){Array.isArray=函数(arg){return Object.prototype.toString.call(arg)==“[Object Array]”;};}//创建阵列的克隆// http://davidwalsh.name/javascript-clone-arrayif(!Array.clone){Array.clone=函数(arg){返回参数切片(0);};}//阵列删除-John Resig(麻省理工学院授权)// http://ejohn.org/blog/javascript-array-remove/if(!Array.remove){if(!Array.remove){Array.remove=函数(arg、from、to){var rest=arg.slice(从到)+1 arg.length);arg.length=从<0?arg.length+自:自;返回参数push.apply(arg,rest);};}/*---JSONP------------------------------------------------------------------*///灵感来自函数jsonp//http://www.west-wind.com/Weblog/posts/107136.aspx//另请参见http://niryariv.wordpress.com/2009/05/05/jsonp-quickly(快速)/// http://en.wikipedia.org/wiki/JSONP函数jsonp(url,回调){if(url.indexOf(“?”)>-1)url+=“&jsonp=”其他的url+=“?jsonp=”url+=回调;//url+=“&”+new Date().getTime().toString();//防止缓存var script=空(“script”);script.setAttribute(“src”,url);script.setAttribute(“类型”,“文本/javascript”);document.body.appendChild(脚本);}var-json={next:0};//与jsonp类似,但不是传递回调函数的名称,而是//直接传递回调函数,从而可以使用匿名//功能。函数jsonpf(url、回调、errorcallback){var name=“callback”+(json.next++);json[name]=函数(x){delete json[name];回调(x);}jsonp(url,“json.”+名称);}/*---AJAX-------------------------------------------------------------------*/函数GetXmlHttpObject(处理程序){var objXMLHttp=空if(window.XMLHttpRequest){//请参见http://www.w3.org/TR/XMLHttpRequest网站/// https://developer.mozilla.org/en/xmlhttprequest网站objXMLHttp=新的XMLHttp请求()}else if(window.ActiveXObject){objXMLHttp=新ActiveXObject(“Microsoft.XMLHTTP”)}返回objXMLHttp}函数ajaxhttp(方法、url、正文、内容类型、回调、errorcallback){var http=GetXmlHttpObject()if(!http){var errortext=“浏览器不支持HTTP请求”;if(errorcallback)errorcallback(errortext,500)else警报(错误文本)}其他{函数statechange(){if(http.readyState==4||http.reatyState==“complete”){如果(http.status<300)回调(http.responseText,http.status);else if(errorcallback)errorcallback(http.responseText,http.status,http.getResponseHeader(“内容类型”);else警报(“请求”+url+“失败:”+http.status+“”+http.statusText);}}http.onreadystatechange=状态更改;http.open(方法、url、true)if(contenttype!=null){http.setRequestHeader(“内容类型”,内容类型)}http.send(正文)}返回http}函数ajaxhttpget(url、回调、errorcallback){ajax_http(“GET”,url,空,空,回调,errorcallback)}函数ajaxhttppost(url、formdata、回调、errorcallback){ajax_http(“POST”、url、formdata、null、回调、errorcallback)//请参见https://developer.mozilla.org/En/XMLHttpRequest/Using_XMLHttp请求#Using_FormData_objects}//通过AJAX的JSON函数ajaxhttpgetjson(url、cont、errorcallback){ajaxhttpget(url,带json(cont,errorcallback),errorcallback;}函数ajaxhttppost_json(url、formdata、cont、errorcallback){ajaxhttp_post(url、formdata、with_json(cont、errorcallback)、errorcallback等);}函数ajaxhttppost_querystring_json(url、querystring、cont、errorcallback){ajax_http(“POST”,url,querystring,“application/x-www-form-urlencoded”,with_json(cont,errorcallback),errorcallback;}带json的函数(cont,errorcallback){返回函数(txt){if(文本){尝试{var json=eval(“(”+txt+“)”)}捕捉(e){if(errorcallback)errorcallback(“JSON解析问题”,500,“text/plain”)返回}cont(json);}其他{if(errorcallback)errorcallback(“空响应表单服务器(崩溃?)”,500,“text/plain”)}}}函数sameOrigin(url){var a=空(“a”);a.href=url;//转换为绝对URLreturn hasPrefix(a.href,location.protocol+“//”+location.host+“/”);}/*//尽可能使用AJAX,回退到JSONP函数http_get_json(url、cont、errorcallback){if(sameOrigin(url))ajax_http_get_json(url,cont,errorcallback);else jsonpf(url,cont,errorcallback);}*///为了更好地处理错误,请始终使用AJAX,不要使用JSONP//PGF服务允许跨原请求,并且在中受支持//所有现代浏览器。//请参见http://en.wikipedia.org/wiki/Cross-origin_resource_sharing函数http_get_json(url、cont、errorcallback){ajaxhttpgetjson(url、cont、errorcallback);}/*---URL构造--------------------------------------------------------*/函数encodeArgs(args){var q=“”for(参数中的var arg)if(args[arg]!=未定义)q+=“&”+arg+“=”+编码URI组件(args[arg]);返回q;}/*--HTML构造----------------------------------------------------*/函数文本{return document.createTextNode;}功能节点(标记、as、ds){var n=属性(as,document.createElement(tag));if(ds)for(ds中的var i)n.appendChild(ds[i]);返回n;}函数attr(as,n){for(var a in as)n.setAttribute(a,as[a]);返回n}函数为空(标记、名称、值){var el=节点(标记,{},[])if(name&&value)el.setAttribute(name,value);返回el;}函数emptyid(标记,id){返回空(标记,“id”,id);}函数empty_class(标记,cls){return empty(标记,“类”,cls函数divid(id,cs){返回节点(“div”,{id:id},cs);}函数spanid(id){返回空id(“span”,id);}函数换行(标记,内容){返回节点(标记,{},Array.isArray(contents)?目录:[目录]);}函数wrap_class(标记、cls、内容){返回节点(标记,{“类”:cls},内容?数组.isArray(内容)?目录:[目录]:[])}函数span_class(cls,contents){return wrap_class函数divclass(cls,contents){return wrap_class(“div”,cls,内容);}函数p(内容){返回包装(“p”,内容);}函数dt(contents){return-wrap(“dt”,contents);}函数dd(contents){return-wrap(“dd”,contents);}函数li(contents){return wrap(“li”,contents);}函数th(contents){返回换行符(“th”,contents);}函数td(contents){return wrap(“td”,contents);}函数tr(单元格){return-wrap(“tr”,单元格);}功能按钮(标签、动作、按键){var el=节点(“input”,{“type”:“button”,“value”:标签},[]);if(typeof action==“string”)el.setAttribute(“onclick”,action);else el.onclick=动作;if(key)el.setAttribute(“accesskey”,key);返回el;}函数选项(标签、值){返回节点(“选项”,{“值”:值},[文本(标签)]);}隐藏函数(名称、值){返回节点(“input”,{type:“hidden”,name:name,value:value},[])}函数tda(cs){返回节点(“td”,{},cs);}函数img(src){return empty(“img”,“src”,src);}函数标题(t,n){return attr({title:t},n)}/*---文件修改------------------------------------------------*/函数清除(el){replaceInnerHTML(el,“”);}函数replaceInnerHTML(el,html){if(el)el.innerHTML=html;}函数replaceChildren(el,newchild){clear(el);el.appendChild(newchild函数appendChildren(el,ds){for(ds中的var i)el.appendChild(ds[i]);返回el;}函数replaceNode(el,ref){ref.parentNode.replaceChild(el,ref)}函数insertFirst(父级、子级){parent.insertBefore(child,parent.firstChild);}函数insertBefore(el,ref){ref.parentNode.insertBefore[el,ref];}函数insertAfter(el,ref){ref.parentNode.insertBefore(el,ref.nextSibling);}功能切换隐藏(el){if(el.classList.contains(“隐藏”))el.classList.remove(“隐藏”)其他的el.classList.add(“隐藏”)}//更新允许多个选择的菜单中的选定选项功能更新MultiMenu(菜单,选择){var set=toSet(选择)var os=菜单选项对于(var i=0;i";}返回结果;}函数字段名称(obj){var结果=“”;for(obj中的var i){结果+=“”+i;}返回结果;}/*---数据操作----------------------------------------------------*/函数交换(a,i,j){//注意:这不适用于字符串。var tmp=a[i];a[i]=a[j];a[j]=tmp;返回a;}函数排序(a){// https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array/sort返回a.sort();/*//注意:这对字符串不起作用。对于(var i=0;i-->