html添加tooltip

來源:互聯網
上載者:User

需求:給表單每個資訊項添加一個協助資訊,當mouseover或focus時啟用協助資訊
效果:
程式實現:

/*
    2007-01-30 lisq custom tooltip
    use age:
    <script src="/modules/commons/js/prototype.js"></script>
    <script src="/modules/commons/js/tooltip.js"></script>
    var config = new ToolTip.Config($('A0101'), '人員姓名', 400)
    var arrConfig = []
    arrConfig.push(config)
    var tootip = ToolTip.Init(arrConfig)
*/
Event.observe(window, 'load', function(){
    var div = document.createElement('div')
    div.id = 'ToolTipID'
    var divIntroTopLine = document.createElement('div')
    divIntroTopLine.id = 'divIntroTopLine'
    div.appendChild(divIntroTopLine)
    
    var divIntroArrow = document.createElement('div')
    divIntroArrow.id = 'divIntroArrow'
    divIntroTopLine.appendChild(divIntroArrow)

    var divIntroContent = document.createElement('div')
    divIntroContent.id = 'divIntroContent'
    divIntroContent.innerHTML = 'hello world'
    div.appendChild(divIntroContent)
    
    ToolTip.Container = div
    ToolTip.ContainerContent = divIntroContent
    document.body.appendChild(div)
    Element.hide(div)
}, false)

var ToolTip = {
    Config : function(ele, tip, width){
        this.ele = ele
        ele.config = this
        this.tip = tip
        this.width = width
    },
    Init : function(arrConfig){
        for(var i=0; i<arrConfig.length; i++){
            var config = arrConfig[i]
            if(!config || !config.ele || !config.tip)
                continue
            config.ele.tip = config.tip
            config.ele.onmouseover = ToolTip.MouseOver
            config.ele.onmouseout = ToolTip.MouseOut
            config.ele.onfocus = ToolTip.MouseOver
            config.ele.onblur = ToolTip.MouseOut
        }
    },
    MouseOver : function(){
        ToolTip.Container.style.width = this.config.width
        ToolTip.ContainerContent.innerHTML = this.tip
        var arr = Position.positionedOffset(this)
        eToolTip = $('ToolTipID')
        eToolTip.style.left = arr[0] - 50
        eToolTip.style.top = arr[1] + 21
        Element.show(eToolTip)
    },
    MouseOut : function(){
        eToolTip = $('ToolTipID')
        Element.hide(eToolTip)
    }
}

調用:

var config = new ToolTip.Config(fieldInput, field.HelpInfo, 150)
            ToolTip.Init([config])

第二種方法:
JavaScript部分tooltip.js

//添加支援,如為某DIV則僅為該DIV中帶有TOOLTIP屬性的元素提供支援。
function addTipSupport(e) {
 e.onmouseover = showTip;
 e.onmouseout = hideTip;
 e.onmousemove = showTip;
}
//獲得控制項的絕對位置,返回左上方座標
function getElePos(e){
 var t=e.offsetTop;
 var l=e.offsetLeft;
 while(e=e.offsetParent){
 t+=e.offsetTop;
 l+=e.offsetLeft;
 }
 
 return [l, t];
}
//顯示TOOLTIP
function showTip() {
 getElePos(event.srcElement);
 if(event.srcElement.tooltip && event.srcElement.tooltip!='') {
 altlayer.style.visibility='visible';
 var cord = getElePos(event.srcElement);
 altlayer.style.left=cord[0]+event.offsetX + 20; //據左顯示的位置
 altlayer.style.top=cord[1]+event.offsetY+25;  //據上顯示的位置
 altlayer.innerHTML=event.srcElement.tooltip;
 altlayer.style.zIndex = event.srcElement.style.zIndex + 1;
 }
 //else altlayer.style.visibility='hidden';
}
//隱藏TOOLTIP
function hideTip() {
 altlayer.style.visibility = 'hidden';
}
//設定顯示地區
addTipSupport(document.body); //此處的參數可換成其它,以局限在某容器內支援TOOLTIP風格。
//////////Tooltip結束//////////  

html部分

<!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>tooltip</title><script type="text/javascript" src="tooltip.js"></script>
</head>
<body>
<div style="visibility:hidden;border:2px groove orange;padding:5px;font-size:12px;background-color:#990000;position:absolute;" id=altlayer></div><!-- 這裡的tooltip即是要顯示到id為altlayer的div中的內容 -->
<img src="http://www.baidu.com/img/logo-yy.gif" tooltip="It's a multiline tooltip!<br>And you can use pictures too!   < br><img src='http://www.baidu.com/img/logo-yy.gif'/>"  onmouseover="showTip()" onmouseout="hideTip()" ></img>
</body>
</html>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.