⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ajax.js

📁 一个用ajax作的树插件
💻 JS
字号:
function $(id)
{
	return document.getElementById(id);	
}
function echo(obj,html)
{
	$(obj).innerHTML=html;
}
function createxmlhttp()
{
	var xmlhttp = false;
	try	
	{
  		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 	} 
	catch (e) 
	{
  		try 
  		{
   			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  		} 
		catch (e) 
		{
   			xmlhttp = false;
 		}
 	}
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') 
	{
  		xmlhttp = new XMLHttpRequest();
		if (xmlhttp.overrideMimeType)
		{
			//设置MiME类别 
			xmlhttp.overrideMimeType('text/xml');
		}
	}
	return xmlhttp;	
}
//向服务器获取数据
function getdata(url,obj,initJS)
{
		var xmlhttp = createxmlhttp();
		xmlhttp.onreadystatechange=requestdata;
		xmlhttp.open("GET",url,true);
		xmlhttp.setRequestHeader("If-Modified-Since","0");
		xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		xmlhttp.send(null);
		function requestdata()
		{
			echo(obj,"<IMG SRC='Images/loading.gif' />&nbsp;<span style=' font-size:12px; color:Black;'>loading.....</span><br>");
			if(xmlhttp.readyState==4)
			{
				if(xmlhttp.status==200)
				{
					echo(obj,xmlhttp.responseText);
					if(initJS != null)
					{
					    getInitJS(initJS);
					}
				}
			}
		}
}
//向服务器发送数据
function postdata(url,obj,data)
{
		var xmlhttp= createxmlhttp();
		xmlhttp.onreadystatechange=requestdata;
		xmlhttp.open("POST", url, true);
		xmlhttp.setRequestHeader("If-Modified-Since","0");
		xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		xmlhttp.send(data);
		function requestdata()
		{
			echo(obj,"<IMG SRC='Images/loading.gif' />&nbsp;<span style=' font-size:12px; color:Black;'>loading.....</span><br>");
			if(xmlhttp.readyState==4)
			{
				if(xmlhttp.status==200)
				{
					if(!postInitJS(xmlhttp.responseText))
					{
					    echo(obj,xmlhttp.responseText);
					}
				}
			}
		}
}

function ShowSub(id,flag)
{
    if(flag=="1")
    {
        eval("sub" + id).style.display='';
        document.images["AddImg" + id].src='Images/subImg.bmp';
        return;
    }
    if(flag=="0")
    {
        eval("sub" + id).style.display='none';
        document.images["AddImg" + id].src='Images/addImg.bmp';
        return;
    }
    if(eval("sub" + id).style.display=='none')
    {
        eval("sub" + id).style.display='';
        document.images["AddImg" + id].src='Images/subImg.bmp';
        return;
    }
    else
    {
        eval("sub" + id).style.display='none';
        document.images["AddImg" + id].src='Images/addImg.bmp';
        return;
    }    
}

function showMenu(id,fatherID)
{ 
    contextmenu.style.posLeft = document.body.scrollLeft + event.x + 10 ;
    contextmenu.style.posTop = document.body.scrollTop + event.y + 10;
    var menuHtml = "";
    menuHtml = menuHtml + "<span style='cursor:hand;font-size:12px; color:Black;' onmousemove=this.style.backgroundColor='#00cccc'; onmouseout=this.style.backgroundColor=''; onclick=getdata('tree.aspx?mode=getAdd&fatherID=" + fatherID + "','add" + fatherID + "','addFocus" + fatherID + "');closeMenu(); >添加同级</span><br />";
    menuHtml = menuHtml + "<span style='cursor:hand;font-size:12px; color:Black;' onmousemove=this.style.backgroundColor='#00cccc'; onmouseout=this.style.backgroundColor=''; onclick=getdata('tree.aspx?mode=addSub&id=" + id + "','addSub" + id + "','getTree" + id + "');closeMenu(); >添加子级</span><br />";
    menuHtml = menuHtml + "<span style='cursor:hand;font-size:12px; color:Black;' onmousemove=this.style.backgroundColor='#00cccc'; onmouseout=this.style.backgroundColor=''; onclick=getdata('tree.aspx?mode=getEdit&id=" + id + "','edit" + id + "','editFocus" + id + "');closeMenu(); >修  改</span><br />";
    menuHtml = menuHtml + "<span style='cursor:hand;font-size:12px; color:Black;' onmousemove=this.style.backgroundColor='#00cccc'; onmouseout=this.style.backgroundColor=''; onclick=closeMenu();if(confirm('你确定要删除此菜单吗?')){postdata('tree.aspx?mode=del&id=" + id + "&fatherID=" + fatherID + "','edit" + id + "');} >删  除</span>";
    document.getElementById('contextmenu').innerHTML = menuHtml ;
    contextmenu.style.display = "" ;
}

function closeMenu()
{
    contextmenu.style.display="none";
}

document.oncontextmenu=function()
{
    return false;
}

document.onclick=function()
{
    if(document.activeElement!=contextmenu)
    {
        closeMenu();
    }
}
function getInitJS(JS)
{
    if(JS.substr(0,6)=='addSub')
    {
        id=JS.substr(6,JS.length);
        getdata('tree.aspx?mode=getAdd&fatherID=' + id,'add' + id,'addFocus' + id);
    }
    if(JS.substr(0,8)=='addFocus')
    {
        id=JS.substr(8,JS.length);
        $('addName'+id).select();
    }
    if(JS.substr(0,9)=='editFocus')
    {
        id=JS.substr(9,JS.length);
        $('editName'+id).select();
    }
    if(JS.substr(0,7)=='getTree')
    {
        id=JS.substr(7,JS.length);
        getdata('tree.aspx?mode=getTree&fatherID=' + id,'sub' + id,'addSub' + id);
        ShowSub(id,'1');
    }
    if(JS.substr(0,10)=='getSubTree')
    {
        id=JS.substr(10,JS.length);
        getdata('tree.aspx?mode=getTree&fatherID=' + id,'sub' + id);
        ShowSub(id,'1');
    }
}
function postInitJS(JS)
{
    if(JS.substr(0,5)=='added')
    {
        id=JS.substr(5,JS.length);
        if(id=="0")
        {
            getdata("tree.aspx?mode=getTree&fatherID=0","divAjax");
        }
        else
        {
            getdata("tree.aspx?mode=getTree&fatherID=" + id,"sub" + id );
        }
        return true;
    }
    if(JS.substr(0,6)=='edited')
    {
        id=JS.substr(6,JS.length);
        getdata('tree.aspx?mode=returnEdit&id=' + id,'edit' + id );
        return true;
    }
    if(JS.substr(0,10)=='addNameRep')
    {
        id=JS.substr(10,JS.length);
        alert('此菜单名已经存在');
        getdata('tree.aspx?mode=getAdd&fatherID=' + id ,'add' + id ,'addFocus' + id);
        return true;
    }
    if(JS.substr(0,11)=='editNameRep')
    {
        id=JS.substr(11,JS.length);
        alert('此菜单名已经存在');
        getdata('tree.aspx?mode=getEdit&id=' + id ,'edit' + id ,'editFocus' + id);
        return true;
    }
    if(JS.substr(0,10)=='deletError')
    {
        id=JS.substr(10,JS.length);
        alert('请先删除此菜单的子级项');
        if(id=="0")
        {
            getdata("tree.aspx?mode=getTree&fatherID=0","divAjax");
        }
        else
        {
            getdata("tree.aspx?mode=getTree&fatherID=" + id,"sub" + id );
        }
        return true;
    }
    if(JS.substr(0,7)=='deleted')
    {
        id=JS.substr(7,JS.length);
        if(id=="0")
        {
            getdata("tree.aspx?mode=getTree&fatherID=0","divAjax");
        }
        else
        {
            getdata("tree.aspx?mode=getTree&fatherID=" + id,"sub" + id );
        }
        return true;
    }
    if(JS.substr(0,5)=='moved')
    {
        id=JS.substr(5,(JS.indexOf('i')-5));
        idFNO=JS.substr(JS.indexOf('i')+1,(JS.indexOf('f')-JS.indexOf('i')-1));
        subID=JS.substr((JS.indexOf('f')+1),JS.length);
        if(idFNO=="0")
        {
            getdata("tree.aspx?mode=getTree&fatherID=0","divAjax");
        }
        else if(document.getElementById("sub" + idFNO)!=null)
        {
            getdata("tree.aspx?mode=getTree&fatherID=" + idFNO,"sub" + idFNO);
        }
        if(id=="0")
        {
            getdata("tree.aspx?mode=getTree&fatherID=0","divAjax",'getSubTree'+subID);
        }
        else if(document.getElementById("sub" + id)!=null)
        {
            getdata("tree.aspx?mode=getTree&fatherID=" + id,"sub" + id,'getSubTree'+subID);
        }
        return true;
    }
    return false;
}

function trim(str) 
{
    return str.replace(/\s+$|^\s+/g,"");
}


function postAdd(id)
{
    if(trim($('addName'+id).value)=='')
    {
        alert('菜单名字不能为空!');
        $('addName'+id).focus();
        return false;
    }
    data="Name="+($('addName'+id).value);
    postdata('tree.aspx?mode=saveAdd&fatherID=' + id ,'add' + id,data);
}

function postEdit(id)
{
    if(trim($('editName'+id).value)=='')
    {
        alert('菜单名字不能为空!');
        $('editName'+id).focus();
        return false;
    }
    data="Name="+($('editName'+id).value);
    postdata('tree.aspx?mode=saveEdit&id=' + id ,'edit' + id,data);
}

function postMove(id,fatherID)
{
    if(id==''||id==null)
    {
        alert('移动失败!');
        return false;
    }
    if(fatherID==''||fatherID==null)
    {
        alert('移动失败!');
        return false;
    }
    data="id="+id+"&fatherID="+fatherID;
    postdata('tree.aspx?mode=move','sub' + fatherID,data);
}
var g_CatchDiv = false;
var g_objNO;
var g_objName;
function catchBDiv(obj,name)
{
    window.setTimeout('g_CatchDiv = true',200)
    g_objNO = obj.substr(4,obj.length);
    g_objName = name;
    var BMDiv = document.getElementById("bDiv");
    var MoveObj = document.getElementById(obj);
    BMDiv.innerHTML=MoveObj.innerHTML;
    BMDiv.style.cursor='';
    BMDiv.style.filter='alpha(opacity=40)';
    BMDiv.style.left = event.x-25;
    BMDiv.style.top = event.y+10;
    BMDiv.setCapture();
    document.onmouseup = function(){releaseDiv();};
    document.onmousemove = function(){moveBDiv();};
}

function releaseDiv()
{
    window.setTimeout('g_CatchDiv = false',200)
    var BMDiv = document.getElementById("bDiv");
    BMDiv.style.display='none';
    BMDiv.releaseCapture();
    document.onmousemove = null;
}

function moveBDiv()
{
    if(g_CatchDiv)
    {
        if(document.images["AddImg" + g_objNO] !=null)
        {
            ShowSub(g_objNO,"0");
        }
        var BMDiv = document.getElementById("BDiv");
        BMDiv.style.left = event.x-25;
        BMDiv.style.top = event.y;
        BMDiv.style.display='';
    }
}

function menuClick(value)
{
    alert(value);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -