首页 | 互联网 | IT动态 | Cisco | Windows | Linux | Java | .Net | Oracle | 华为 | 存储世界 | 服务器 | 网络设备 | IDC | 安全 | 求职招聘
IT培训 | 数字网校 | 技术专题 | 电子书下载 | 教学视频 | 网页设计 | 平面设计 | 解决方案 | 直播室 | 虚拟考场 | 搜索 | 博客 | 沙龙 | 论坛
中国IT实验室Linux频道
中国IT教育
 
首页 资讯动态 认证考试 新手入门 核心技术 高级技术 J2EE J2ME Java&XML 开源技术 其他技术 RSS订阅 论坛 专题
您现在的位置: 中国IT实验室 >> Java >> 核心技术 >> 高级编程 >> 文章正文

给大家讲个不错的ajax组件

文章来源csdn博客 作者佚名 更新时间2008-5-5 保存本文保存本文 推荐给好友推荐给好友 收藏本页收藏本页

    function Xajax()
    {
    if (xajaxDebug) this.DebugMessage = function(text) { alert("Xajax Debug:\n " + text) };

    this.workId = 'xajaxWork'+ new Date().getTime();
    this.depth = 0;

    //Get the XMLHttpRequest Object
    this.getRequestObject = function()
    {
    if (xajaxDebug) this.DebugMessage("Initializing Request Object..");
    var req;
    try
    {
    req=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e)
    {
    try
    {
    req=new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch (e2)
    {
    req=null;
    }
    }
    if(!req && typeof XMLHttpRequest != "undefined")
    req = new XMLHttpRequest();

    if (xajaxDebug) {
    if (!req) this.DebugMessage("Request Object Instantiation failed.");
    }

    return req;
    }

    // xajax.$() is shorthand for document.getElementById()
    this.$ = function(sId)//$是什么?
    {
    return document.getElementById(sId);
    }

    // xajax.include(sFileName) dynamically includes an external javascript file
    this.include = function(sFileName)
    {
    var objHead = document.getElementsByTagName('head');
    var objScript = document.createElement('script');
    objScript.type = 'text/javascript';
    objScript.src = sFileName;
    objHead[0].appendChild(objScript);
    }

    // xajax.addHandler adds an event handler to an element
    this.addHandler = function(sElementId, sEvent, sFunctionName)
    {
    if (window.addEventListener)
    {
    //eval的含义?
    eval("this.$('"+sElementId+"').addEventListener('"+sEvent+"',"+sFunctionName+",false);");
    }
    else
    {
    eval("this.$('"+sElementId+"').attachEvent('on"+sEvent+"',"+sFunctionName+",fal**);");
    }
    }

    // *ajax.removeHandler removes an event handler from an element
    this.removeHandler = function(sElementId, sEvent, sFunctionName)
    {
    if (window.addEventListener)
    {
    eval("this.$('"+sElementId+"').removeEventListener('"+sEvent+"',"+sFunctionName+",false);");
    }
    else
    {
    eval("this.$('"+sElementId+"').detachEvent('on"+sEvent+"',"+sFunctionName+",fal**);");
    }
    }

    // *ajax.create creates a new child node under a parent
    this.create = function(sParentId, sTag, sId)
    {
    var objParent = this.$(sParentId);
    objElement = document.createElement(sTag);
    objElement.setAttribute('id',sId);
    objParent.appendChild(objElement);
    }

    // xajax.insert inserts a new node before another node
    this.insert = function(sBeforeId, sTag, sId)
    {
    var objSibling = this.$(sBeforeId);
    objElement = document.createElement(sTag);
    objElement.setAttribute('id',sId);
    objSibling.parentNode.insertBefore(objElement, objSibling);
    }

    this.getInput = function(sType, sName, sId)
    {
    var Obj;
    if (sType == "radio" && !window.addEventListener)
    {
    alert('here');
    Obj = document.createElement('<input type="radio" id="'+sId+'" name="'+sName+'">');
    }
    else
    {
    Obj = document.createElement('input');
    Obj.setAttribute('type',sType);
    Obj.setAttribute('name',sName);
    Obj.setAttribute('id',sId);
    }
    return Obj;
    }

    // xajax.createInput creates a new input node under a parent
    this.createInput = function(sParentId, sType, sName, sId)
    {
    var objParent = this.$(sParentId);
    var objElement = this.getInput(sType, sName, sId);
    objParent.appendChild(objElement);
    }

    // xajax.insertInput creates a new input node before another node
    this.insertInput = function(sBeforeId, sType, sName, sId)
    {
    var objSibling = this.$(sBeforeId);
    var objElement = this.getInput(sType, sName, sId);
    objSibling.parentNode.insertBefore(objElement, objSibling);
    }

    // xajax.remove deletes an element
    this.remove = function(sId)
    {
    objElement = this.$(sId);
    if (objElement.parentNode && objElement.parentNode.removeChild)
    {
    objElement.parentNode.removeChild(objElement);
    }
    }

    //xajax.replace searches for text in an attribute of an element and replaces it
    //with a different text
    this.replace = function(sId,sAttribute,sSearch,sReplace)
    {
    var bFunction = false;

    eval("var txt=document.getElementById('"+sId+"')."+sAttribute);
    if (typeof txt == "function")
            {
                txt = txt.toString();
                bFunction = true;
            }
    if (txt.indexOf(sSearch)>-1)
    {
    var newTxt = '';
    while (txt.indexOf(sSearch) > -1)
    {
    x = txt.indexOf(sSearch)+sSearch.length+1;
    newTxt += txt.substr(0,x).replace(sSearch,sReplace);
    txt = txt.substr(x,txt.length-x);
    }
    newTxt += txt;
    if (bFunction)
    {
    eval("newTxt =" + newTxt);
    eval('this.$("'+sId+'").'+sAttribute+'=newTxt;');
    }
    else if (this.willChange(sId,sAttribute,newTxt))
    {
    eval('this.$("'+sId+'").'+sAttribute+'=newTxt;');
    }
    }
    }

[1] [2] [3] [4] 下一页  

【责编:Ken】

中国IT教育

相关产品和培训
文章评论
 友情推荐链接
 认证培训
 专题推荐

 ·XML全攻略技术专题
 ·JAVA开源技术介绍专题
 ·Java嵌入式开发之J2ME技术专题
 ·超前体验 Oracle 11g的5个新特性
 ·揭密使用VB.NET的五个实用技巧
 ·Oracle和SQL Server常用函数对比专题
 ·展现C#世界 C#程序设计专题
 ·Java入门 Tomcat的配置技巧精华专题
 ·Oracle RMAN物理备份技术详解
 ·JAVA开发利器——JBuilder知多少
 今日更新
 社区讨论
 博客论点
 频道精选
 Java 频道导航