$(function(){//dom 加载完执行 // 设定图片层距离鼠标的位置 var x = 10; var y = 20; $("a.tooltip").mouseover(function(e){// 选择 class 为 tooltip 的 a 标记,添加鼠标经过事件的方法 this.myTitle = this.title;// 得到 a 标记的 title 属性 this.title = "";// 把原来的 title 属性设为空字符串 var imageTitle = this.myTitle ? (" " + this.myTitle) : "";// 判断 myTitle 是否为空,不为空给它前面加 br var im = "" + imageTitle + "";// 创建一个 div $("body").append(im);// 把创建的 div 追加到 body 中 // 设定追加的 div 的位置, e 是鼠标的位置,再加上距离鼠标的位置 ,就是 div 的位置 $("#tooltip").css("left", e.pageX + x + "px") .css("top", e.pageY + y + "px") .show("fast");// 显示 速度设为 快 }).mouseout(function(){// 鼠标移出事件的方法 this.title = this.myTitle;// 把 myTitle 还给 title $("#tooltip").remove();// 删除新添加的 div }).mousemove(function(e){// 鼠标在图片上移动的事件的方法 // 修正位置 $("#tooltip").css("left", e.pageX + x + "px") .css("top", e.pageY + y + "px"); }).click(function(){return false;});// 取消 a 标记的默认方法 }) script>