Ajax+PHP 边学边练 之二 实例
前端之家收集整理的这篇文章主要介绍了
Ajax+PHP 边学边练 之二 实例,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
效果1. 当鼠标放在某日上时,如果当天有备忘录,则会显示出来,如下图:
<A href="/upload/2009-11/20091124221629523.png">
<div class="codetitle"><a style="CURSOR: pointer" data="4114" class="copybut" id="copybut4114" onclick="doCopy('code4114')"> 代码如下:
<div class="codebody" id="code4114">
function checkfortasks (thedate,e){
//找到
页面中task
Box对应
设置为可见
theObject = document.getElementById("task
Box");
theObject.style.visibility = "visible";
//初始化task
Box位置
var posx = 0;
var posy = 0;
//定位task
Box位置为鼠标位置
posx = e.clientX + document.body.scrollLeft;
posy = e.clientY + document.body.scrollTop;
theObject.style.left = posx + "px";
theObject.style.top = posy + "px";
//设置
PHP请求
页面 serverPage = "taskchecker.
PHP?thedate=" + thedate;
//设置
PHP返回数据替换位置
objID = "task
Box";
var obj = document.getElementById(objID);
//发送请求并加载返回数据
xmlhttp.open("GET",serverPage);
xmlhttp.onreadystatechange = function(){
if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
obj.innerHTML = xmlhttp.responseText;
}
}
xmlhttp.send(null);
}
效果2. 当鼠标点击某日录入姓名时,系统会
自动检索姓名是否存在,并可以通过选择填入姓名框中,如图:
<A href="/upload/2009-11/20091124221629982.png">
<div class="codetitle">
<a style="CURSOR: pointer" data="95828" class="copybut" id="copybut95828" onclick="doCopy('code95828')"> 代码如下: 位置
theObject = document.getElementById("autocompletediv");
//设置为可见
theObject.style.visibility = "visible";
theObject.style.width = "152px";
//设置检索
标签位置
var posx = 0;
var posy = 0; posx = (findPosX (document.getElementById("yourname")) + 1);
posy = (findPosY (document.getElementById("yourname")) + 23); theObject.style.left = posx + "px";
theObject.style.top = posy + "px";
//设定事件为
键盘录入
var theextrachar = e.which; if (theextrachar == undefined){
theextrachar = e.keyCode;
}
//设定加载检索名单位置
var objID = "autocompletediv"; //设定
PHP请求
页面,并将
用户输入的姓名传值过去(同时考虑到Backspace作用)
if (theextrachar == 8){
if (thevalue.length == 1){
var serverPage = "autocomp.
PHP";
}
else{
var serverPage = "autocomp.
PHP" + "?sstring=" + thevalue.substr(0,(thevalue.length -1));
}
}
else{
var serverPage = "autocomp.
PHP" + "?sstring=" + thevalue + String.fromCharCode(theextrachar);
}
//发送请求并加载返回数据
var obj = document.getElementById(objID);
xmlhttp.open("GET",serverPage);
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
obj.innerHTML = xmlhttp.responseText;
}
}
xmlhttp.send(null);
}