工具提示插件可以定制元素的提示外观,提示内容支持变量、Ajax远程获取,还可以自定义提示内容显示的位置,它的调用格式如下:
$(selector).tooltip({options});
其中selector为需要显示提示信息的元素,可选项参数options为tooltip()方法的配置对象,在该对象中,可以设置提示信息的弹出、隐藏时的效果和所在位置。
例如,将三个<a>元素与工具提示插件相绑定,当把鼠标移动在<a>元素内容时,以动画效果弹出对应的提示图片,移出时,图片自动隐藏,如下图所示:
在浏览器中显示的效果:
从图中可以看出,由于各个<a>元素都绑定了工具提示插件,因此,将在指定的位置并以动画效果展示各个<a>元素中title属性所对应的内容。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>工具提示插件</title> <link href="http://www.imooc.com/data/jquery-ui.css" rel="stylesheet" type="text/css" /> <link href="style.css" rel="stylesheet" type="text/css" /> <script src="http://www.imooc.com/data/jquery-1.8.2.min.js" type="text/javascript"></script> <script src="http://www.imooc.com/data/jquery-ui-1.9.2.min.js" type="text/javascript"></script> </head> <body> <div id="divtest"> <div class="title"> 工具提示插件</div> <div class="content"> <div> <label for="name"> 姓名</label> <input id="name" name="name" title="我是土豪,欢迎与我做朋友" /> </div> </div> </div> <script type="text/javascript"> $(function () { ?({ show: { effect: "slideDown", delay: 350 }, hide: { effect: "explode", delay: 350 }, position: { my: "left top", at: "left bottom" } }); }); </script> </body> </html>
body { font-size: 13px; } #divtest { width: 382px; } #divtest .title { padding: 8px; background-color: Blue; color: #fff; height: 23px; line-height: 23px; font-size: 15px; font-weight: bold; } #divtest .content { padding: 8px 0px; background-color: #fff; font-size: 13px; } #divtest a { width: 127px; } .fl { float: left; } .fr { float: right; }