自定义 Title

HTML 为我们提供了 title,用起来的确很方便,同时又加入了一些“特殊效果”,比如:渐显 和 延时 等等。

但有很多时候,我则认为有一钟“画蛇添足”的 味道。尤其是延时,换句话说就是反应慢。如果对于一个单独的 Control 使用了 title,也许感触不深。但如果将其使用在一个 Table 的 一列 value 中,你会有一钟反应迟钝的感觉。

因此引出这篇文章,相对于 HTML 提供的 Title,也许有几点改进,当然反应速度是第一条,还有就是还可以定义 这个 title Template 的 style。

本文共列出两段代码,可以说是几乎完全相同,但同时,尚有一些小的差别,希望在使用的同时能够仔细比较,也许你有意外收获。

代码段一:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title></title>
</head>
<body>
<span alt="AAA">AAA</span><br><br>
<span alt="BBB" altbg="red" altcolor=yellow altborder="yellow">BBB</span><br><br>
<span alt="CCC" altbg="#F7F7F7" altcolor="#999999" altborder="#CCCCCC">CCC</span><br><br>
<span alt="DDD" altbg="green" altcolor=yellow altborder="darkgreen">DDD</span><br><br>
<span alt="EEE" altbg="#000000" altcolor="#FFFFFF" altborder="#000000">EEE</span>
<div style="display:none;border:1px solid #000000;background-color:#FFFFCC;font-size:12px;position:absolute;padding:2;" id=altlayer></div>
<script>
document.body.onmousemove
=quickalt;
document.body.onmouseover
=getalt;
document.body.onmouseout
=restorealt;
var tempalt='';

function getalt()
{
    
if(event.srcElement.alt && (event.srcElement.alt!='' || tempalt!=''))
    {
        altlayer.style.left
=event.x; // 比较下面的代码。
        altlayer.style.top=event.y;  // 比较下面的代码。
        altlayer.style.display='';
        tempalt
=event.srcElement.alt;
        tempbg
=event.srcElement.altbg;
        tempcolor
=event.srcElement.altcolor;
        tempborder
=event.srcElement.altborder;
        event.srcElement.alt
='';
        altlayer.innerText
=tempalt;
        
if (typeof(tempbg)!="undefined"){altlayer.style.background=tempbg}else{altlayer.style.background="#FFFFCC"}
        
if (typeof(tempcolor)!="undefined"){altlayer.style.color=tempcolor}else{altlayer.style.color=tempcolor="#000000"}
        
if (typeof(tempborder)!="undefined"){altlayer.style.border='1px solid '+tempborder;}else{altlayer.style.border='1px solid #000000';}
    }
}
function quickalt()
{
    
if(altlayer.style.display=='')
    {
        altlayer.style.left
=event.x; // 比较下面的代码。
        altlayer.style.top=event.y;  // 比较下面的代码。
    }
}

function restorealt()
{
    event.srcElement.alt
=tempalt;
    tempalt
='';
    altlayer.style.display
='none';
}
</script>
</body>
</html>

代码段二:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title></title>
</head>
<body>
<span title="Java &lt;br&gt;script" title="">Default style</span><br><br> /* 注意 title 换行 */
<span title="<marquee style='100px;'>www.51windows.net</marquee>" altbg="red" altcolor="yellow" altborder="yellow">marquee text</span><br><br>
<span title="<img src='http://www.10cn.net/IMA/net.gif' border='0'>" altbg="#F7F7F7" altcolor="#999999" altborder="#CCCCCC">photo graph</span><br><br>
<span title="<i style='font-size:24pt;font-family:verdana;'>www.10cn.net</i>" altbg="green" altcolor="yellow" altborder="darkgreen">big font</span><br><br>
<div style="display:none;border:1px solid #000000;background-color:#FFFFCC;font-size:12px;position:absolute;padding:2;" id=altlayer></div>
<SCRIPT LANGUAGE="JavaScript">
<!--
document.body.onmousemove
=quickalt;
document.body.onmouseover
=getalt;
document.body.onmouseout
=restorealt;
var tempalt='';

function getalt()
{
    
if(event.srcElement.title && (event.srcElement.title!='' || (event.srcElement.title=='' && tempalt!='')))
    {
        altlayer.style.left
=document.body.scrollLeft+event.clientX;
        altlayer.style.top
=document.body.scrollTop+event.clientY;
        altlayer.style.display
='';
        tempalt
=event.srcElement.title;
        tempbg
=event.srcElement.altbg;
        tempcolor
=event.srcElement.altcolor;
        tempborder
=event.srcElement.altborder;
        event.srcElement.title
='';
        altlayer.innerHTML
=tempalt;
        
if (typeof(tempbg)!="undefined"){altlayer.style.background=tempbg}else{altlayer.style.background="infobackground"}
        
if (typeof(tempcolor)!="undefined"){altlayer.style.color=tempcolor}else{altlayer.style.color=tempcolor="infotext"}
        
if (typeof(tempborder)!="undefined"){altlayer.style.border='1px solid '+tempborder;}else{altlayer.style.border='1px solid #000000';}
    }
}
function quickalt()
{
    
if(altlayer.style.display=='')
    {
        altlayer.style.left
=document.body.scrollLeft+event.clientX;
        altlayer.style.top
=document.body.scrollTop+event.clientY;
    }
}
function restorealt()
{
    event.srcElement.title
=tempalt;
    tempalt
='';
    altlayer.style.display
='none';
}
//-->
</SCRIPT>
</body>
</html>

原文地址:https://www.cnblogs.com/publicbill/p/414672.html