javascript 框架小结 个人工作经验


Posted in Javascript onJune 13, 2009

/**************************************************************************************** 作者:萧 枫
QQ:77182997
MSN:xiaofengnet@hotmail.com
Email:xiaofengnet@163.com
网址:http://www.d369.net
请保留版权

谢谢合作
版本:V 1.6.1
/*****************************************************************************************
/*
为XiaoFeng.System添加一方法Scroll [2009-02-18] V 1.4.1
为Array添加一方法Index [2009-04-09] V 1.4.2
为String 添加TrimStart,TrimEnd,Trim,StartsWith,EndsWith函数 [2009-04-09] V 1.4.3
为 Number 添加 toFixed 方法 实现 数字保留小数点后几位 [2009-04-17] V 1.4.4
FileType 获得文件的名称及文件的后缀名 [2009-04-20] V 1.4.5
XiaoFeng.Dialog 打开选择窗口及保存窗 [2009-05-28] V 1.4.6
通过Object类为每个对象添加方法getType 获得对象的父结点 getParent[2009-06-04] V 1.4.7
更新方法 getQuery(s) U默认为当前地址栏地址,s为传进来要提取的参数 getQuery(U,s) U 为传进来的地址 s 为传进来要提取的参数 [2009-06-08] V 1.4.8
为String 添加两 方法 wTh全角转换为半角 和 hTw半角转换为全解 [2009-06-08] V 1.4.8
更改了ajax类中的 当正在处理这段时间内的一个方法 ajax.Fun = function(){},以及判断服务器处理程序是否出错的一属性 ajax.Error [2009-06-09] V 1.4.9
重新写了String,Array,Object,Element的方法 [2009-06-12] V 1.5.0
添加了捕捉事件源 getElement XiaoFeng.getElement [2009-06-12] V 1.5.1
更新了 加载XML函数 兼容 FF IE LoadXml() [2009-06-12] V 1.5.2
添加功能 无限级下拉列表 SelectClass [2009-06-13] V 1.6.0
更新了 无限级下拉列表的 选择为空时 选择的值 为上一级下拉列表的值 [2009-06-13] V 1.6.1
*/
/*========================================================================================
框架核心内容--------【基础工具类】
========================================================================================
*/
if(!window.XiaoFeng || !XiaoFeng || typeof XiaoFeng == "undefined")var XiaoFeng = window.XiaoFeng = new Object();
var Prototype = {
Version: "1.4.9",
ScriptFragment: '(?:<script.*?>)((\n|\r|.)*?)(?:<\/script>)',
emptyFunction: function(){},
K: function(e){return e}
}
/*========================================================================================
【为Object类添加静态方法】
创建于[2008-08-13]
为Object类添加静态方法:extend
*/
Object.extend = function(destination,source){
for(property in source)destination[property] = source[property];
return destination;
}
//通过Object类为每个对象添加方法extend
Object.prototype.extend = function(object){return Object.extend.apply(this,[this,object]);}
/*===========================================================================================
【给数组添加属性】
[创建于2009-06-12]
对数组添加附加属性
*/
Object.extend(Array.prototype,{
remove : function(N){//移除指定的元素
if(isNaN(N)||N>this.length)return false;
for(var i=0,n=0;i<this.length;i++)if(i!=N)this[n++] = this[i];
this.length -= 1;
},
add : function(v){//添加一数组元素
for(var i=0;i<this.length;i++)
if(this[i].toString() == v.toString()) break;
this.push(v);
},
index : function(s){//指定数组中元素的索引
var FlagIndex = -1;
for(var i = 0;i < this.length; i++)
if(this[i].toString() == s.toString()){FlagIndex = i; break;}
return FlagIndex;
},
first : function(){return this[0];},//数组中第一个元素
last : function(){return this[this.length - 1];},//数组中最后一个元素
clear : function(){this.length = 0; return this;}//清空数组中的元素
});
/*===========================================================================================
【给字符串添加属性】
[创建于2009-06-12]
给字符串加附加属性
*/
Object.extend(String.prototype,{
len : function(){return this.replace(/[^\x00-\xff]/g,"ya").length;},//字符串的长度,一个汉字为两个
Length : function(){
var M = 0;
for(var i=0;i<this.length;i++){
if(this.charCodeAt(i) > 255)
M = M + 2;
else
M = M + 1;
}
return M;
},
Trim : function(s){return this.TrimStart(s).TrimEnd(s);},//清空开始与结尾的指定字符 
TrimStart : function(s){//清空开始的指定字符
if(!s)s = "\\s+";
var trimStartStr = new RegExp("^("+ s +")+","g");
return this.replace(trimStartStr,"");
},
TrimEnd : function(s){//清空结尾的指定字符
if (!s)s = "\\s+";
var trimEndStr = new RegExp("("+ s +")+$","g");
return this.replace(trimEndStr,"");
},
StartsWith : function(s){
if (!s)s = "\\s";
var startsWithStr = new RegExp("^("+ s +")","g");
return startsWithStr.test(this);
},
EndsWith : function(s){
if (!s)s = "\\s";
var endsWithStr = new RegExp("("+ s +")$","g");
return endsWithStr.test(this);
},
wTh : function(){//全角转换半角
var s = "";
for(var i = 0;i < this.length; i++)
s += this.charCodeAt(i) >= 65248?String.fromCharCode(this.charCodeAt(i) - 65248):this.charAt(i);
return s;
},
hTw : function(){//半角转换全角
var s = "";
for(var i = 0;i < this.length; i++)
s += this.charCodeAt(i) < 65248?String.fromCharCode(this.charCodeAt(i) + 65248):this.charAt(i);
return s;
},
LeftStr : function(M){//左边指定长度字符
if(this.Length() > M){
var str = "";
for(var i=0;i<this.length;i++){
if(this.charCodeAt(i) > 255)
M -= 2;
else
M -= 1;
str += this.substring(i,i+1);
if(M <= 0)break;
}
return str + "...";
}else
return this;
},
stripTags : function(){return this.replace(/<\/?[^>]+>/gi, '');}
});
/*===========================================================================================
【取得数字小数点后几位】
[创建于2009-06-12]
为数字添加附加属性
*/
Object.extend(Number.prototype,{
toFixed : function(N){//格式化数字
if(arguments.length == 0)N = 2;
with(Math){var m = pow(10,Number(N));var s = (round(this*m)/m).toString();}
if(s.indexOf('.') < 0){
s += ".";
s += "000000000000000000000000000000";
}
return s.substr(0,s.indexOf('.') + N + 1);
}
});
/*========================================================================================
【获取一个指定ID的结点】
创建于[2005-05-03]
document.getElementById(Id)获取一个指定ID的结点,是这个方法的快捷方式和扩展可以指定多个参
数返回一个对象数组。参数也不一定是ID也可以是对象本身的引用,例如$("id")等价于$($("id"))
*/
var $ = XiaoFeng.$ = function(){
var elements = new Array();
for(var i=0;i<arguments.length;i++){
var element = arguments[i];
if (typeof(element) == "string") element = document.getElementById?document.getElementById(element):document.all.element
if (arguments.length==1) return element;
elements.push(element);
}
return elements;
}
/*========================================================================================
【获取一个指定ID的结点】
创建于[2005-05-03]
document.getElementsByName(id)获取一个指定ID的名称集,是这个方法的快捷方式和扩展可以指定多个参数返回一个对象数组
*/
var $N = XiaoFeng.$N = function(){
var elements = new Array();
for(var i=0;i<arguments.length;i++){
var element=arguments[i];
if (typeof(element) == "string") element = document.getElementsByName?document.getElementsByName(element):document.all.element;
if (arguments.length==1) return element;
elements.push(element);
}
return elements;
}
/*========================================================================================
【获取一个指定TagName的结点】
创建于[2005-05-03]
document.getElementsByTagName(TagName)获取一个指定TagName的名称集,是这个方法的快捷方式和扩展可以指定多个参数返回一个对象数组
*/
var $T = XiaoFeng.$T = function(){
var elements = new Array();
for(var i=0;i<arguments.length;i++){
var element = arguments[i];
if (typeof(element) == "string") element = document.getElementsByTagName(element);
if (arguments.length==1) return element;
elements.push(element);
}
return elements;
}
/*========================================================================================
【创建一个元素】
创建于[2008-06-18]
*/
var $C = XiaoFeng.$C = function(){return document.createElement(arguments[0]);}
/*===========================================================================================
【转换数组】
创建于[2008-08-13]
*/
var $A = XiaoFeng.$A = function(a){return a?Array.apply(null,a):new Array;}
/*===========================================================================================
【抓取事件源】
创建于[2008-08-13]
*/
var getElement = XiaoFeng.getElement = function(){
if(arguments.length == 0)
return event.srcElement;
else
return document.all?arguments[0].srcElement:arguments[0].target;
}
/*========================================================================================
【为Object类添加事件及卸载事件及鼠标坐标】
创建于[2008-10-09]
为Object类添加事件及卸载事件及鼠标坐标:
*/
Object.extend(Object.prototype,{
addEvent : function(a, b, c, d){
//添加函数
if(a.attachEvent)a.attachEvent(b[0], c);
else a.addEventListener(b[1] || b[0].replace(/^on/, ""), c, d || false);
return c;
},
delEvent : function(a, b, c, d){
if(a.detachEvent) a.detachEvent(b[0], c);
else a.removeEventListener(b[1] || b[0].replace(/^on/, ""), c, d || false);
return c;
},
Event : function(){//获取Event
return window.event ? window.event : (function (o){
do{
o = o.caller;
} while (o && !/^\[object[ A-Za-z]*Event\]$/.test(o.arguments[0]));
return o.arguments[0];
})(this.reEvent);
},
Scroll : function(){
return {
Left : document.body.scrollLeft == 0?document.documentElement.scrollLeft:document.body.scrollLeft,
Top :document.body.scrollTop == 0?document.documentElement.scrollTop:document.body.scrollTop
};
}
});
/*========================================================================================
【判断浏览器及其版本号】
[创建于2009-06-12]
*/
XiaoFeng.userAgent = function(){
var ua = navigator.userAgent.toLowerCase();
if(window.ActiveXObject)return {name : "IE",ver : ua.match(/msie ([\d.]+)/)[1]}
if(document.getBoxObjectFor)return {name : "Firefox",ver : ua.match(/firefox\/([\d.]+)/)[1]}
if(window.MessageEvent && !document.getBoxObjectFor)return {name : "Chrome",ver : ua.match(/chrome\/([\d.]+)/)[1]}
if(window.opera)return {name : "Opera",ver : ua.match(/opera.([\d.]+)/)[1]}
if(window.openDatabase)return {name : "Safari",ver : ua.match(/version\/([\d.]+)/)[1]}
return {name : "Other",ver : 0}
}
/*========================================================================================
【鼠标的相对坐标】
[创建于2008-10-06]
*/
var Event = function(){
var e = Object.Event();
var o = {x : 0,y : 0};
switch(arguments[0]){
case 0 :/*鼠标的相对位置*/
o.x = e.clientX;
o.y = e.clientY;
break;
case 1 :/*鼠标的绝对位置*/
o.x = e.clientX + Object.Scroll().Left;
o.y = e.clientY + Object.Scroll().Top;
break;
default:/*鼠标的相对位置*/
o.x = e.clientX;
o.y = e.clientY;
break;
}
return o;
}
/*========================================================================================
【Ajax创建类】
创建于[2008-05-30]
更新于[2009-06-09]
var ajax = new AjaxRequest();
ajax.Url = "Test.aspx";
ajax.Content = "";
ajax.Fun = function(){alert("提取中..\");}
ajax.CallBack = function(e){
//操作代码
var XmlDoc=e.responseText;
var XmlDoc=e.responseXML;
var Roots=XmlDoc.documentElement.childNodes;
alert(Roots[0].childNodes[0].text);
}
ajax.Send();
*/
XiaoFeng.Ajax = AjaxRequest;
function AjaxRequest(){
var xmlhttp = false;
var self = this;
this.Method = "post";
this.Url = "";
this.Content = "";
this.Async = true;
this.Fun = function(){return;};
this.CallBack = function(obj){return;}
this.Error = null;
this.Create = function(){//创建XMLHttpRequest
if(typeof(window.XMLHttpRequest)!="undefined"){
xmlhttp = new XMLHttpRequest();
if(xmlhttp.overrideMimeType)xmlhttp.overrideMimeType("text/html");
}else if(window.ActiveXObject){
var Versions = ["MSXML2.XMLHttp.5.0","MSXML2.XMLHttp.4.0","MSXML2.XMLHttp.3.0","MSXML2.XMLHttp","Microsoft.XMLHttp"];
for(var i=0;i<Versions.length;i++){
try{
var XmlHttp = new ActiveXObject(Versions[i]);
xmlhttp = XmlHttp;
}catch(Error){
if(i == (Versions.length-1))alert("出错:创建服务器对象实例失败.");//浏览器问题//抛出Error.description
}
}
}
return xmlhttp;
}
this.Send = function(){
if(this.Url == ""){alert("处理地址不能为空!");}
xmlhttp = this.Create();
xmlhttp.open(this.Method,this.Url,true);
if(this.Method == "post")xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState == 4){
if(xmlhttp.status == 200){
self.CallBack(xmlhttp);
}else{
alert("出错:错误代码["+ xmlhttp.status +"]");
self.Error = "服务器出错["+ xmlhttp.status +"].";
clipboardData.setData("text",self.Url+"?"+self.Content);
self.CallBack(self);
}
}else
self.Fun();
}
if(this.Method.toLowerCase() == "post")
xmlhttp.send(this.Content);
else
xmlhttp.send(null);
}
}
/*========================================================================================
【创建XML对象类】
[创建于2008-04-09]
var Http_Request=false;
*/
function CreateAjax(){
var Ajax_Obj;
if(typeof(window.XMLHttpRequest) != "undefined"){
Ajax_Obj = new XMLHttpRequest();
if(Ajax_Obj.overrideMimeType)Ajax_Obj.overrideMimeType("text/html");
}else if(window.ActiveXObject){
var Versions = ["MSXML2.XMLHttp.5.0","MSXML2.XMLHttp.4.0","MSXML2.XMLHttp.3.0","MSXML2.XMLHttp","Microsoft.XMLHttp"];
for(var i=0;i<Versions.length;i++){
try{
var XmlHttp=new ActiveXObject(Versions[i]);
Ajax_Obj = XmlHttp;
}catch(Error){
if(i == (Versions.length-1))alert("出错:创建服务器对象实例失败[浏览器问题].");//浏览器问题//抛出Error.description
}
}
}
return Ajax_Obj;
}
/*========================================================================================
【加载XML操作类】
[创建于2009-06-12]
*/
function LoadXml(Path){
if(XiaoFeng.userAgent().name == "Firefox"){
var Dom = document.implementation.createDocument("", "", null);
Dom.async = false;
Dom.load(Path);
}else{
Dom = new ActiveXObject("Microsoft.XMLDOM");
Dom.async = false;
Dom.load(Path);
}
return Dom;
}
/*========================================================================================
【Cookies操作类】
[创建于2008-04-09]
*/
var Cookies = {
GetVal:function(offset){//获得Cookie解码后的值
var endstr = document.cookie.indexOf(";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
},
Add:function(name,value,hours){//设定Cookie值
var expire = "";
if(hours != null){
expire = new Date((new Date()).getTime() + hours * 3600000);
expire = "; expires=" + expire.toGMTString();
}
document.cookie = name + "=" + escape(value) + expire;
},
Del:function(name){//删除Cookie
var exp = new Date();
exp.setTime(exp.getTime() - 1);
var cval = GetCookie (name);
document.cookie = name + "=" + cval + "; expires="+ exp.toGMTString();
},
Get:function(name){//获得Cookie的原始值
var cookieValue = "";
var search = name + "=";
if(document.cookie.length > 0){
offset = document.cookie.indexOf(search);
if (offset != -1){
offset += search.length;
end = document.cookie.indexOf(";", offset);
if (end == -1) end = document.cookie.length;
cookieValue = unescape(document.cookie.substring(offset, end))
}
}
return cookieValue;
},
get:function(name){/*获取Cookie的原始值 注 在这里主要是对应C#里面的Cookies数组*/
var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
var r = Cookies.Get("HTGL").match(reg);
if (r != null) return unescape(r[2]);
return "";
}
}
/*//======================================================================================
//【获得对象实际坐标】
[创建于2008-04-09]
*/
function getDim(e){
var rd = {x:0,y:0};
do{
rd.x += e.offsetLeft;
rd.y += e.offsetTop;
e = e.offsetParent;
}while(e)
return rd;
}
/*//======================================================================================
//【获得对象的父结点】
[创建于2008-06-04]
*/
function getParent(o,N){
var e = new Object();
e = o;
do{
e = e.parentNode;
try{if(e.tagName.toLowerCase() == N.toLowerCase())break;}catch(e){break;}
}while(e)
return e;
}
/*//======================================================================================
//【获得对象实际四角坐标】
[创建于2008-10-09]
*/
function getInfo(o){//取得坐标
var to = new Object();
to.left = to.right=to.top=to.bottom=0;
var twidth = o.offsetWidth;
var theight = o.offsetHeight;
do{
to.left += o.offsetLeft;
to.top += o.offsetTop;
o = o.offsetParent;
}while(o != document.body)
to.right = to.left + twidth;

to.bottom = to.top + theight;
return to;
}
/*//======================================================================================
//【获得对象与指定字符匹配的对象】
[创建于2008-10-09]
*/
function getObj(o,s){
f = false;
while(o != document.body){
if(o.id.toLowerCase().indexOf(s) != -1){
f = true;
break;
}
o = o.offsetParent;
}
return f;
}
/*========================================================================================
【获得参数值】
[创建于2008-06-08]
*/
var getQuery = XiaoFeng.getQuery = function(){
if(arguments.length == 0)return null;
if(arguments.length == 1){
var reg = new RegExp("(^|&)"+ arguments[0] +"=([^&]*)(&|$)");
var r = window.location.search.substr(1).match(reg);
if (r!=null) return unescape(r[2]);
return null;
}else{
var Reg = new RegExp(arguments[1] +"=\\w+","gi");
try{return arguments[0].match(Reg).toString().split(",")[0].split("=")[1];}catch(e){return null;}
}
}
/*========================================================================================
【拖动函数】
创建于[2008-04-09]
*/
var Drag = XiaoFeng.Drag = function(o){
var x,y;
x = getDim(o).x;
y = getDim(o).y;
var deltaX = event.clientX - x;
var deltaY=event.clientY - y;
var drag = true;
o.style.filter = "Alpha(Opacity=60)";
o.onmousemove = function(){
if(drag){
o.style.left=(event.clientX - deltaX) + "px";
o.style.top=(event.clientY - deltaY) + "px";
if(event.clientX - deltaX <= 0)o.style.left = 0 + "px";
if(event.clientY - deltaY <= 0)o.style.top = 0 + "px";
if(event.clientX - deltaX + o.offsetWidth >= XiaoFeng.System.Size(0).x)o.style.left = (XiaoFeng.System.Size(0).x - o.offsetWidth) + "px";
if(event.clientY - deltaY + o.offsetHeight >= XiaoFeng.System.Size(0).y)o.style.top = (XiaoFeng.System.Size(0).y - o.offsetHeight) + "px";
o.setCapture();
}
}
o.onmouseup = function(){
o.style.filter = "Alpha(Opacity=100)";
drag = false;
o.releaseCapture();
}
}
/*========================================================================================
【字符转换类】
创建于[2008-04-09]
更新于[2008-06-02]
*/
var StrToHtml = {
sTh:function(s){
s = s.replace(/</gi,"<");
s = s.replace(/>/gi,">");
s = s.replace(/"/gi,"\"");
s = s.replace(/&/gi,"&");
s = s.replace(/ /gi," ");
return s;
},
hTs:function(s){
s = s.replace(/</gi,"<");
s = s.replace(/>/gi,">");
s = s.replace(/\\/gi,""");
s = s.replace(/&/gi,"&");
s = s.replace(/ /gi," ");
return s;
},
bTy:function(s){
s = s.replace(/\[/gi,"<");
s = s.replace(/\]/gi,">");
return s;
},
yTb:function(s){
s = s.replace(/</gi,"[");
s = s.replace(/</gi,"[");
s = s.replace(/>/gi,"]");
s = s.replace(/>/gi,"]");
s = s.replace(/ /gi," ");
return s;
}
}
/*===========================================================================================
【获得窗体的宽高】
主要是一些系统方法
*/
XiaoFeng.System = {
Event : function(){
var e = Object.Event();
var o = {x : 0,y : 0};
switch(arguments[0]){
case 0 :/*鼠标的相对位置*/
o.x = e.clientX;
o.y = e.clientY;
break;
case 1 :/*鼠标的绝对位置*/
o.x = e.clientX + Object.Scroll().Left;
o.y = e.clientY + Object.Scroll().Top;
break;
default:/*鼠标的相对位置*/
o.x = e.clientX;
o.y = e.clientY;
break;
}
return o;
},
Size : function(){//【获得窗体的宽高】
var s = {x : 0,y : 0};
if(window.innerWidth){
s.x = window.innerWidth;
s.y = window.innerHeight;
}else if(document.compatMode=='CSS1Compat'){
if(arguments[0] == 1){
s.x = document.body.clientWidth;
s.y = document.body.clientHeight;
}else if(arguments[0] == 2){
s.x = parseInt(document.documentElement.clientWidth) + parseInt(document.documentElement.scrollLeft);
s.y = parseInt(document.documentElement.clientHeight) + parseInt(document.documentElement.scrollTop);
}else{
s.x = document.documentElement.clientWidth;
s.y = document.documentElement.clientHeight;
}
}else if(document.body){
if(arguments[0] == 1){
s.x = document.body.scrollWidth;
s.y = document.body.scrollHeight
}else if(arguments[0] == 2){
s.x = document.body.clientWidth + document.body.scrollLeft;
s.y = document.body.clientHeight + document.body.scrollTop;
}else{
s.x = document.body.clientWidth;
s.y = document.body.clientHeight;
}
}
return s;
},
Scroll : function(){//【获得窗体的滚动条】
var s = {x : 0,y : 0};
if(document.compatMode=='CSS1Compat'){
s.x = document.documentElement.scrollTop;
s.y = document.documentElement.scrollLeft;
}else if(document.body){
s.x = document.body.scrollTop;
s.y = document.body.scrollLeft;
}
return s;
},
getRnd : function(){return Math.floor(Math.random()*1000000);}
}
/*===========================================================================================
【获得文件的名称及文件的后缀名】
[2009-04-20]
*/
function FileType(FName){
FName = FName.replace(/\\/gi,"/");
FName = FName.substr(FName.lastIndexOf("/") + 1);
return {Name : FName.substr(0,FName.indexOf(".")),Type : FName.substr(FName.indexOf(".") + 1)};
}
/*===========================================================================================
【打开选择窗口及保存窗口】
[2009-05-28]
第一种方法:
var dialog = new XiaoFeng.Dialog("请选择文件","txt 文件(*.txt)|*.txt|所有文件(*.*)|*.*",".txt");
第二种方法:
var dialog = new XiaoFeng.Dialog({
Title : "请选择文件",
Filter : "txt 文件(*.txt)|*.txt|所有文件(*.*)|*.*",
DefaultExt : ".txt"
});
dialog.Open();
注:参数可以不填写,则为默认的.
*/
XiaoFeng.Dialog = function(){
this.DialogTitle = "请选择要打开的文件";
this.DialogFilter = "Excel 文件(*.xls)|*.xls|所有文件(*.*)|*.*";
this.DialogDefaultExt = ".xls";
this.Query = arguments;
var self = this;
this.Init = function(){
if(typeof self.Query[0] == "string"){
self.DialogTitle = self.Query[0]?self.Query[0]:self.DialogTitle;
self.DialogFilter = self.Query[1]?self.Query[1]:self.DialogFilter;
self.DialogDefaultExt = self.Query[2]?self.Query[2]:self.DialogDefaultExt;
}else if(typeof self.Query[0] == "object"){
self.DialogTitle = self.Query[0].Title?self.Query[0].Title:self.DialogTitle;
self.DialogFilter = self.Query[0].Filter?self.Query[0].Filter:self.DialogFilter;
self.DialogDefaultExt = self.Query[0].DefaultExt?self.Query[0].DefaultExt:self.DialogDefaultExt;
}
try{
if(!$("Dialog_OpenSave")){
var _Dialog_Open = $C("object");
_Dialog_Open.id = "Dialog_OpenSave";
_Dialog_Open.classid = "CLSID:F9043C85-F6F2-101A-A3C9-08002B2F49FB";
_Dialog_Open.style.display = "none";
document.body.appendChild(_Dialog_Open);
}
$("Dialog_OpenSave").CancelError = true;
}catch(e){}
}
this.Open = function(){
$("Dialog_OpenSave").DialogTitle = this.DialogTitle;
$("Dialog_OpenSave").Filter = this.DialogFilter;
$("Dialog_OpenSave").DefaultExt = this.DialogDefaultExt;
$("Dialog_OpenSave").ShowOpen();
return $("Dialog_OpenSave").FileName;
}
this.Save = function(){
$("Dialog_OpenSave").DialogTitle = this.DialogTitle.replace("打开","保存");
$("Dialog_OpenSave").Filter = this.DialogFilter;
$("Dialog_OpenSave").DefaultExt = this.DialogDefaultExt;
$("Dialog_OpenSave").ShowSave();
return $("Dialog_OpenSave").FileName;
}
this.Init();
}
/*===========================================================================================
【无限级下拉列表】
[2009-06-13]
在要放置下拉列表的地方放置一控件,一般为隐藏域 <input type="hidden" name="ClassId" id="ClassId" />
以XML为数据源可以用直接调用 XML 形式
var XMLDOM = LoadXml("CreateXML.ashx?RootId=15&sd=101");
Root = XMLDOM.documentElement;
也可以用Ajax调出数据源
var ajax = new XiaoFeng.AjaxRequest();
ajax.Url = "CreateXML.ashx?RootId=15&sd=101";
ajax.CallBack = function(e){
if(e.Error != "undefined")return;
var Root = e.responseXML.documentElement;
}
var ClassSelect = new SelectClass();
ClassSelect.Source = Root;
ClassSelect.Name = "ClassId";
ClassSelect.FirstOption = [["==请选择==","0"]];
ClassSelect.SelectFirst = true;
ClassSelect.Fun = function(e){}
ClassSelect.Select = "101";
ClassSelect.Run();

参数说明:Source 为本下拉列表数据源 为 XMLDOM
Name 为隐藏控件ID 一般为隐藏域
FirstOption 为下拉列表第一行显示的文字,如果想给每个都有不同的可以设置多个 例如[["==请选择所在的省份==","0"],["==请选择所在的市份==","0"],["==请选择所在的县份==","0"]] 如果 FirstOption = "" 或 FirstOption = [] 则不显示这些文字 默认 为[["==请选择==",""]]
SelectFirst 为是加载完后是否显示要设置的文字 true 为显不 false 为显示数据第一行或设置的行 默认为 true
Fun 此函数为每个下拉列表的onchange选择事件触发接口函数
Select 为已经选择的属性 默认为""
Run 为运行此类别
*/
var SelectClass = XiaoFeng.SelectClass = function(){
this.Name = "";
this.Source = new Object();
this.FirstOption = [["==请选择==","0"]];
this.SelectFirst = true;
this.Select = "";
this.Fun = function(e){return;}
this.Run = function(){
if(!$(this.Name)){
var _Input = $C("input");
_Input.type = "hidden";
_Input.name = this.Name;
_Input.id = this.Name;
document.body.appendChild(_Input);
}
$(this.Name).value = this.Select;
if(typeof this.Source == "object")
if(this.Source.hasChildNodes){
this.CreateSelect(this.Source);
if(this.Select == ""){
var __Select = $(this.Name).parentNode.getElementsByTagName("select");
if(this.SelectFirst)
$(this.Name).value = __Select[__Select.length - 1].value;
else
$(this.Name).value = __Select[__Select.length - 1].options[1].value;
}
}else{
var _Span = $C("span");
_Span.innerHTML = "数据源为空。";
$(this.Name).parentNode.appendChild(_Span);
}
else{
var _Span = $C("span");
_Span.innerHTML = "数据源出错。";
$(this.Name).parentNode.appendChild(_Span);
}
}
this.CreateSelect = function(node){
if(!node.hasChildNodes){this._SelectFirstName();return;}
var Select = $C("select");
Select.id = Select.name = "select_"+ (new Date().getTime());
var _f = false,_s = 0;
if(typeof this.FirstOption == "string")this.FirstOption = [];
if(typeof this.FirstOption == "object" && this.FirstOption.length > 0)
Select.add(this.CreateOption("==请选择==","0",false));
for(var i = 0;i < node.childNodes.length; i++){
if(node.childNodes[i].getAttribute("s") == null){_f = false;}else{_f = true;_s = i;}
Select.add(this.CreateOption(node.childNodes[i].getAttribute("Name"),node.childNodes[i].getAttribute("Id"),_f));
}
$(this.Name).parentNode.insertBefore(Select,$(this.Name));
if(_s > 0)
this.CreateSelect(node.childNodes[_s]);
else{
this.CreateSelect(node.childNodes[0]);
if(this.SelectFirst)
if(this.FirstOption.length == 0)
Select.options[0].selected = true;
else
Select.options[1].selected = true;
}
}
this.CreateOption = function(t,v,f){
var OptionSub = new Option();
OptionSub.text = t;
OptionSub.value = v;
if(f)OptionSub.selected = f;
return OptionSub;
}
this._SelectFirstName = function(){
var _Select = $(this.Name).parentNode.getElementsByTagName("select");
var self = this,_f = true;
var _FirstOption = [],_FirstLength = this.FirstOption.length;
if(this.FirstOption.length == 0)_f = false;
for(var i = 0;i < _Select.length; i++){
if(_f){
if(i >= _FirstLength)
_FirstOption = this.FirstOption[0];
else
_FirstOption = this.FirstOption[i];
_Select[i].options[0].text = _FirstOption[0];
_Select[i].options[0].value = _FirstOption[1];
}
if(i == _Select.length - 1)
_Select[i].setAttribute("onchange",function(){
var __Select = this.parentNode.getElementsByTagName("select");
$(self.Name).value = __Select[__Select.length - 1].value;
if(self.FirstOption.length != 0 && this.selectedIndex == 0)
if(_Select.length > 1)
$(self.Name).value = __Select[__Select.length - 2].value;
self.Fun(this);
});
else
_Select[i].setAttribute("onchange",function(){
self._RemoveSelect(this);
self._SelectNode(self.Source,this.value);
var __Select = this.parentNode.getElementsByTagName("select");
if(self.SelectFirst){
$(self.Name).value = __Select[__Select.length - 1].value;
}else
$(self.Name).value = __Select[__Select.length - 1].options[1].value;
if(self.FirstOption.length != 0 && this.selectedIndex == 0)
if(_Select.length > 1)
$(self.Name).value = __Select[__Select.length - 2].value;
self.Fun(this);
});
}
}
this._SelectNode = function(node,s){
for(var i = 0;i < node.childNodes.length; i++)
if(node.childNodes[i].getAttribute("Id") == s)
this.CreateSelect(node.childNodes[i]);
else
if(node.childNodes[i].hasChildNodes)this._SelectNode(node.childNodes[i],s);
}
this._RemoveSelect = function(o){
var _Select = $(this.Name).parentNode.getElementsByTagName("select");
var _f = false;
for(var i = 0;i < _Select.length; i++){
if(_Select[i] == o){_f = true;continue;}
if(_f){_Select[i].parentNode.removeChild(_Select[i]);i--;}
}
}
}

Javascript 相关文章推荐
用js获取电脑信息(是使用与IE浏览器)
Jan 15 Javascript
清除div下面的所有标签的方法
Feb 17 Javascript
jquery控制display属性为none或block
Mar 31 Javascript
2014 HTML5/CSS3热门动画特效TOP10
Dec 07 Javascript
为JS扩展Array.prototype.indexOf引发的问题及解决办法
Jan 21 Javascript
整理Javascript基础入门学习笔记
Nov 29 Javascript
原生js图片轮播效果实现代码
Oct 19 Javascript
使用JS和canvas实现gif动图的停止和播放代码
Sep 01 Javascript
深入理解Node.js中通用基础设计模式
Sep 19 Javascript
Vue+webpack项目基础配置教程
Feb 12 Javascript
每周一练 之 数据结构与算法(Stack)
Apr 16 Javascript
Kettle中使用JavaScrip调用jar包对文件内容进行MD5加密的操作方法
Sep 04 Javascript
动态刷新 dorado树的js代码
Jun 12 #Javascript
firefo xml 读写实现js代码
Jun 11 #Javascript
犀利的js 函数集合
Jun 11 #Javascript
js 操作css实现代码
Jun 11 #Javascript
图片连续滚动代码[兼容IE/firefox]
Jun 11 #Javascript
Javascript Tab 导航插件 (23个)
Jun 11 #Javascript
javascript 树控件 比较好用
Jun 11 #Javascript
You might like
PHP header函数分析详解
2011/08/06 PHP
Mysql的Root密码忘记,查看或修改的解决方法(图文介绍)
2013/06/14 PHP
浅析PHP中Session可能会引起并发问题
2015/07/23 PHP
php进行ip地址掩码运算处理的方法
2016/07/11 PHP
php文件上传 你真的掌握了吗
2016/11/28 PHP
基于jquery的获取浏览器窗口大小的代码
2011/03/28 Javascript
jQuery的each终止或跳过示例代码
2013/12/12 Javascript
JavaScript cookie的设置获取删除详解
2014/02/11 Javascript
JS控制表单提交的方法
2015/07/09 Javascript
Bootstrap基本插件学习笔记之模态对话框(16)
2016/12/08 Javascript
通过命令行生成vue项目框架的方法
2017/07/12 Javascript
jQuery选择器中的特殊符号处理方法
2017/09/08 jQuery
深入理解Vuex 模块化(module)
2017/09/26 Javascript
angular内置provider之$compileProvider详解
2017/09/27 Javascript
详解easyui基于 layui.laydate日期扩展组件
2018/07/18 Javascript
Vue实现左右菜单联动实现代码
2018/08/12 Javascript
JS实现基本的网页计算器功能示例
2020/01/16 Javascript
django自定义Field实现一个字段存储以逗号分隔的字符串
2014/04/27 Python
Python中zip()函数用法实例教程
2014/07/31 Python
Python中在for循环中嵌套使用if和else语句的技巧
2016/06/20 Python
python实现简单神经网络算法
2018/03/10 Python
python实现超市商品销售管理系统
2019/10/25 Python
如何通过python实现全排列
2020/02/11 Python
python高阶函数map()和reduce()实例解析
2020/03/16 Python
Python3.7安装pyaudio教程解析
2020/07/24 Python
关于Kotlin中SAM转换的那些事
2020/09/15 Python
大学毕业自我鉴定范文
2014/02/03 职场文书
学生手册评语
2014/05/05 职场文书
养成教育经验材料
2014/05/26 职场文书
心理咨询专业自荐信
2014/07/07 职场文书
意向书范本
2014/07/29 职场文书
关于安全的广播稿
2014/10/23 职场文书
教师思想工作总结2015
2015/05/13 职场文书
2015年大学宣传部工作总结
2015/05/26 职场文书
MySQL 覆盖索引的优点
2021/05/19 MySQL
SQL Server数据库备份和恢复数据库的全过程
2022/06/14 SQL Server