ajax|訊息提示框|js擷取html絕對位置 封裝

來源:互聯網
上載者:User

         function Ajax(){
   
             //    doc.innerHTML='<span><img src="image/load.gif"/>Loading...</span>';
            ////創造對象
            var xmlhttp;
            try
            {
                if (window.ActiveXObject){
                    /* 不要刪除以下注釋,這部分不是注釋 */
                    /*@cc_on @*/
                    /*@if (@_jscript_version >= 5)
                    try {
                      xmlhttp = new ActiveXObject("Msxml2.xmlhttp");
                    } catch (e) {
                      try {
                        xmlhttp = new ActiveXObject("Microsoft.xmlhttp");
                      } catch (e) {
                        xmlhttp = false;
                      }
                    }
                    @end @*/
                }else{
                    xmlhttp=new XMLHttpRequest();
                }
                if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
                  xmlhttp = new XMLHttpRequest();
                }
            }
            catch(e)
            {
            alert(e.message);
            }
            //alert(xmlhttp);
            if(!xmlhttp)
            {
            alert("你的瀏覽器不支援xmlhttp對象,所以一些功能無法使用,建議使用高版本的瀏覽器!!");
            return;
            }

//    
            ///函數主題
                this.getData=function(url,data,dodata)
                {
                    var verbs =data;
                   // alert(data);
                    xmlhttp.open("POST", url,true);

                        xmlhttp.onreadystatechange=function(){
                            if (xmlhttp.readyState==4){
                                dodata(xmlhttp);                 
                            }
                        }
                    xmlhttp.setRequestHeader("Content-Length",verbs.length);
                    xmlhttp.setRequestHeader("CONTENT-TYPE","application/x-www-form-urlencoded");
                    xmlhttp.send(verbs);
                }
                /////
                this.getUrlData=function(url,data,dodata)
                {
                    xmlhttp.open("GET",url,true);

                    xmlhttp.onreadystatechange = function() {

                    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                      dodata(xmlhttp);
                     Status.setStatusShow(false);
                    }
                    else
                    {
                      Status.showInfo("載入中...");
                    }
                 
                    }
                     xmlhttp.setRequestHeader("CONTENT-TYPE","application/x-www-form-urlencoded");
                    xmlhttp.send(null);
                }
                this.getFormData=function(demo)
                {
                alert(demo);
                }
            ///函數主題
}
/**
 * @author zxub 2006-06-01
 * 狀態資訊顯示類,用var Status=new function()定義,可以靜態引用其中的方法
 * 一般情況下為function Status(),這樣不能靜態引用其中的方法,需要通過對象來引用
 */
var Status=new function()
{
    this.statusDiv=null;
   
    /**
     * 初始化狀態顯示層
     */
 this.init=function()
 {
     if (this.statusDiv!=null)
     {
         return;
     }
  var body = document.getElementsByTagName("body")[0];
  var div = document.createElement("div");
  div.style.position = "absolute";
  div.style.top = "50%";
  div.style.left = "50%";
  div.style.width = "280px";
  div.style.margin = "-50px 0 0 -100px";  
  div.style.padding = "15px";
  div.style.backgroundColor = "#353555";
  div.style.border = "1px solid #CFCFFF";
  div.style.color = "#CFCFFF";
  div.style.fontSize = "14px";
  div.style.textAlign = "center";
  div.id = "idstatus";
  body.appendChild(div);
  div.style.display="none";
  this.statusDiv=document.getElementById("idstatus");
 }
 
 /**
  * 設定狀態資訊
  * @param _message:要顯示的資訊
  */ 
 this.showInfo=function(_message)
 {  
     if (this.statusDiv==null)
     {
         this.init();
     } 
     this.setStatusShow(true);
     this.statusDiv.innerHTML = _message;    
 }
 
 /**
  * 設定狀態層是否顯示
  * @param _show:boolean值,true為顯示,false為不顯示
  */
 this.setStatusShow=function(_show)
 {  
     if (this.statusDiv==null)
     {
         this.init();
     }
     if (_show)
     {
         this.statusDiv.style.display="";
     }
     else
     {
         this.statusDiv.innerHTML="";
         this.statusDiv.style.display="none";
     }
 }
}
function GetPosition(element) {
    var result = new Object();
    result.x = 0;
    result.y = 0;
    result.width = 0;
    result.height = 0;
    if (element.offsetParent) {
        result.x = element.offsetLeft;
        result.y = element.offsetTop;
        var parent = element.offsetParent;
        while (parent) {
            result.x += parent.offsetLeft;
            result.y += parent.offsetTop;
            var parentTagName = parent.tagName.toLowerCase();
            if (parentTagName != "table" &&
                parentTagName != "body" &&
                parentTagName != "html" &&
                parentTagName != "div" &&
                parent.clientTop &&
                parent.clientLeft) {
                result.x += parent.clientLeft;
                result.y += parent.clientTop;
            }
            parent = parent.offsetParent;
        }
    }
    else if (element.left && element.top) {
        result.x = element.left;
        result.y = element.top;
    }
    else {
        if (element.x) {
            result.x = element.x;
        }
        if (element.y) {
            result.y = element.y;
        }
    }
    if (element.offsetWidth && element.offsetHeight) {
        result.width = element.offsetWidth;
        result.height = element.offsetHeight;
    }
    else if (element.style && element.style.pixelWidth && element.style.pixelHeight) {
        result.width = element.style.pixelWidth;
        result.height = element.style.pixelHeight;
    }
    return result;
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.