javascript 框架小结 个人工作经验
时间:2021-07-05 09:20:22|栏目:JavaScript代码|点击: 次
/**************************************************************************************** 作者:萧 枫
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--;}
}
}
}
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代码
本文地址:http://www.codeinn.net/misctech/152850.html