在QQ聊天窗口中,當好友發送給我們一個網址鏈接時,將鼠標移到鏈接上時,會出現一個提示框,提示我們是打開、復制還是舉報。其實在網頁中我們也可以做出這樣的效果,今天就教大家實現仿QQ聊天窗口中點擊鏈接出現提示效果的方法。
關代碼如下所示:
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>仿QQ聊天窗口中點擊鏈接出現提示效果的方法-poluoluo.Com</title>
<script language="JavaScript" type="text/javascript">
function S(i){return document.getElementById(i)}
function copyUrl(){ //復制網址
var clipBoardContent=S("downloadDirect").href;
try{
window.clipboardData.setData("Text",clipBoardContent);
alert("此網址已經復制到剪貼板。");
}catch(e){
alert("復制不成功,請在IE浏覽器下復制。");
}
}
document.onclick = function(evt){ //在不是鏈接的位置單擊時,隱藏顯示的層
var _target = evt ? evt.target : event.srcElement ;
var _id = _target.id;
if( _id == "" ){
_id = _target.tagName;
}
if( _id !="A"){
S("downloadPanel").style.display = 'none';
}
}
window.onload=function(){ //網頁載入時,綁定指定對象下的所有a
var htmer=S("htmer");
var htmer1 = htmer.getElementsByTagName("a");
for(var i=0,j=htmer1.length; i<j; i++){
htmer1[i].onclick=function(evt){
var _event = evt ? evt : window.event;
var _target = evt ? evt.target : window.event.srcElement;
var _p = S("downloadPanel");
_p.style.top = _event.clientY + document.body.scrollTop ;
_p.style.left = ( _event.clientX + document.body.scrollLeft < 160 ? _event.clientX + document.body.scrollLeft + 10 : _event.clientX + document.body.scrollLeft - 120 );
S("downloadPanel").style.display = '';
S("downloadDirect").href= this.href;
return false;
}
}
}
</script>
<style type="text/css">
html,body{ font-size:12px;}
a {color:#0000FF;text-decoration:underline;cursor:pointer;}
.pointer {cursor:pointer;}
.infobar {background:#EAF9FF;border:1px solid #1D5CBB;color:#0000FF;margin-bottom:10px;padding:8px 20px 4px 20px;}
</style>
</head>
<body>
<div id="downloadPanel" style="position:absolute;top:0px;left:0px;width:160px;z-index:999;padding:6px 2px 6px 10px;border:1px solid #1D5CBB;display:none;" class="infobar">
<div style="padding:3px 0 0 0;"><a id="downloadDirect" href="javascript:void(0);" target="_blank">打開網址</a> <a id="downloadAgent" href="javascript:void(0);" onClick="copyUrl()">復制</a> <a href="javascript:void(0)" onClick="S('downloadPanel').style.display = 'none';">關閉</a></div>
</div>
<div id="htmer">
<p><a href="http://www.poluoluo.Com">http://www.poluoluo.Com</a></p>
<p><a href="http://poluoluo.Com">http://poluoluo.Com</a></p>
</div>
</body>
</html>