javascript 框架小结 个人工作经验_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--;}
}
}
}

时间: 2024-10-30 05:49:32

javascript 框架小结 个人工作经验_javascript技巧的相关文章

实例分析浏览器中“JavaScript解析器”的工作原理_javascript技巧

浏览器在读取HTML文件的时候,只有当遇到<script>标签的时候,才会唤醒所谓的"JavaScript解析器"开始工作. JavaScript解析器工作步骤: 1."找一些东西": var. function. 参数:(也被称之为预解析) 备注:如果遇到重名分为以下两种情况: 遇到变量和函数重名了,只留下函数 遇到函数重名了,根据代码的上下文顺序,留下最后一个 2.逐行解读代码. 备注:表达式可以修改预解析的值 JS解析器在执行第一步预解析的时候,会

javascript引用类型指针的工作方式_javascript技巧

先看个例子: <script> var a = {n:1}; var b = a; a.x = a = {n:2}; console.log(a.x);// --> undefined console.log(b.x);// --> [object Object] </script> 上面的例子看似简单,但结果并不好了解,很容易把人们给想绕了--"a.x不是指向对象a了么?为啥log(a.x)是undefined?"."b.x不是应该跟a.

JavaScript 学习小结(适合新手参考)_javascript技巧

常量:JavaScript常量又称字面常量,是固化在程序代码中的信息. 变量:变量的主要作用是存取数据,提供一个存取信息的容器. 数据类型     整型:整型常量就是数字,可以是十进制.八进制.十六进制.     字符串:字符串就是以英文""或''括起来的一个或几个字符.     布尔型:布尔型常量就是逻辑上的"真"和"假".     空值:在JavaScript中,用"null"表示什么都没有. 弹出警告框:alert 弹出

JavaScript创建对象的方式小结(4种方式)_javascript技巧

本文实例总结了JavaScript创建对象的方式.分享给大家供大家参考,具体如下: 潜意识里,JavaScript不能算是面向对象的语言,要算也只能说是趋向面向对象的一种语言,至少它不能很好的吻合面向对象最基本的三大特性(继承.封装.多态),当然有很多人就认为JavaScript是面向对象语言,好像也说得没错,因为面向对象也能在JavaScript中实现,比如说继承.封装吧也都可以在JavaScript中实现,但是实现方便吗?所以我觉得很疑惑.看到网上有一网友评价的很好,"面向对象只是一种思想,

javascript框架设计之框架分类及主要功能_javascript技巧

从内部架构和理念划分,目前JavaScript框架可以划分为5类. 第一种是以命名空间为导向的类库或框架,如果创建一个数组用new Array(),生成一个对象用new Object(),完全的java风格,因此,我们以某一对象为跟,不断为它添加对象和二级对象属性来组织代码,如金字塔般垒起来,早期代表YUI,EXT(so,不是有活力的公司都还用它们) 第二种是以类工厂为导向的框架.著名的有Prototype,还有mootools.Base2.Ten,它们基本上除了最基本的命名空间,其它模块都是一

从javascript语言本身谈项目实战_javascript技巧

从javascript语言本身谈项目实战                                              dulao5  2005-1-15        随着ajax的升温,javascript越来越得到人们的重视.重要的是,ajax在一定程度上带来了web软件架构上的变化,人们把越来越多的功能分配到客户端实现,javascript子项目规模越来越大.如何更高效的使用javascript,如何更科学的组织javascript,如何更顺利的保证项目进展?我想就我的经验谈

Javascript核心读书有感之语句_javascript技巧

在javascript中,表达式是短语,那么语句(statement)就是整句或命令.正如英文语句以句号结尾,javascript以分号结尾. 表达式计算出一个值,但语句使某件事发生. "使某件事发生"的一个方法是计算带有副作用的表达式.诸如赋值和函数调用这些有副作用的表达式,是可以作为单独的语句的.这种把表达式当做语句的用法也称做表达式语句(expression statement).类似的语句还有声明语句(declaration statement),声明语句用来声明新变量或者定义

javascript实现控制文字大中小显示_javascript技巧

部分网站内容页通常会看到有控制文字分别以 大,中,小 三种方式显示,下面就把这个小功能做一下记录,对提高网站用户体验度还是有一些帮助的哦! <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>js控制文字大中小显示</title> </head> <body&g

浅谈JavaScript 覆盖原型以及更改原型_javascript技巧

覆盖原型 //囚犯示例 //1.定义原型对象 var proto = { sentence : 4, //监禁年限 probation: 2 //缓刑年限 }; //2.定义原型对象的构造函数 var Prisoner = function(name, id) { this.name = name; this.id = id; }; //3.将构造函数关联到原型 Prisoner.prototype = proto; //4.实例化对象--采用工厂函数实例化对象 var makePrisoner