asp.net中自定义tooltip的显示

来源:互联网 发布:淘宝上哪家零食店好 编辑:程序博客网 时间:2024/05/16 19:40

本文根据网上的一篇文章加工而成的

1.建一个js文件(tooltip.js)

//--初始化变量--
var rT=true;//允许图像过渡
var bT=true;//允许图像淡入淡出
var tw=150;//提示框宽度
var endaction=false;//结束动画

var ns4 = document.layers;
var ns6 = document.getElementById && !document.all;
var ie4 = document.all;
offsetX = 0;
offsetY = 20;
var toolTipSTYLE="";
function initToolTips()
{
  if(ns4||ns6||ie4)
  {
    if(ns4) toolTipSTYLE = document.toolTipLayer;
    else if(ns6) toolTipSTYLE = document.getElementById("toolTipLayer").style;
    else if(ie4) toolTipSTYLE = document.all.toolTipLayer.style;
    if(ns4) document.captureEvents(Event.MOUSEMOVE);
    else
    {
      toolTipSTYLE.visibility = "visible";
      toolTipSTYLE.display = "none";
    }
    document.onmousemove = moveToMouseLoc;
  }
}
function toolTip(msg, fg, bg)
{
  if(toolTip.arguments.length < 1) // hide
  {
    if(ns4)
    {
    toolTipSTYLE.visibility = "hidden";
    }
    else
    {
      //--图象过渡,淡出处理--
      if (!endaction) {toolTipSTYLE.display = "none";}
      if (rT) document.all("msg1").filters[1].Apply();
      if (bT) document.all("msg1").filters[2].Apply();
      document.all("msg1").filters[0].opacity=0;
      if (rT) document.all("msg1").filters[1].Play();
      if (bT) document.all("msg1").filters[2].Play();
      if (rT){
      if (document.all("msg1").filters[1].status==1 || document.all("msg1").filters[1].status==0){ 
      toolTipSTYLE.display = "none";}
      }
      if (bT){
      if (document.all("msg1").filters[2].status==1 || document.all("msg1").filters[2].status==0){ 
      toolTipSTYLE.display = "none";}
      }
      if (!rT && !bT) toolTipSTYLE.display = "none";
      //----------------------
    }
  }
  else // show
  {
    if(!fg) fg = "#777777";
    if(!bg) bg = "#eeeeee";
    var content =
    '<table id="msg1" name="msg1" border="0" cellspacing="0" cellpadding="1" bgcolor="' + fg + '" class="trans_msg"><td>' +
    '<table border="0" cellspacing="0" cellpadding="3" bgcolor="' + bg +
    '"><td width=' + tw + '><font face="Arial" color="' + fg +
    '" size="-2">' + msg +
    '&nbsp/;</font></td></table></td></table>';

    if(ns4)
    {
      toolTipSTYLE.document.write(content);
      toolTipSTYLE.document.close();
      toolTipSTYLE.visibility = "visible";
    }
    if(ns6)
    {
      document.getElementById("toolTipLayer").innerHTML = content;
      toolTipSTYLE.display='block'
    }
    if(ie4)
    {
      document.all("toolTipLayer").innerHTML=content;
      toolTipSTYLE.display='block'
      //--图象过渡,淡入处理--
      var cssopaction=document.all("msg1").filters[0].opacity
      document.all("msg1").filters[0].opacity=0;
      if (rT) document.all("msg1").filters[1].Apply();
      if (bT) document.all("msg1").filters[2].Apply();
      document.all("msg1").filters[0].opacity=cssopaction;
      if (rT) document.all("msg1").filters[1].Play();
      if (bT) document.all("msg1").filters[2].Play();
      //----------------------
    }
  }
}
function moveToMouseLoc(e)
{
  if(ns4||ns6)
  {
    x = e.pageX;
    y = e.pageY;
  }
  else
  {
    x = event.x + document.body.scrollLeft;
    y = event.y + document.body.scrollTop;
  }
  toolTipSTYLE.left = x + offsetX;
  toolTipSTYLE.top = y + offsetY;
  return true;
}

2.在aspx页面文件中的<head></head>中加入

<script language="javascript" type="text/javascript" src="tooltip.js"></script>
<style  type="text/css">
<!--
.trans_msg
    {
    filter:alpha(opacity=100,enabled=1) revealTrans(duration=.2,transition=1) blendtrans(duration=.2);
    }
-->
</style>

3.<form></form>中加入下面代码

<div id="toolTipLayer" style="position:absolute; visibility: hidden"></div>
<script>initToolTips()</script>

4.修改aspx.cs代码文件

Page_Load中

 LinkButton1.Attributes.Add("OnMouseOver", "javascript:toolTip('" + "<B>"+query1 +"</B>"+ "')");

//鼠标移入触发的事件toolTip有三个参数,
 LinkButton1.Attributes.Add("OnMouseOut", "javascript:toolTip()");//鼠标移开事件
  
 顺便介绍一下,如何保护js文件,使客户端不能下载js文件

“虚拟目录”右键“属性”----“配置”----在映射中“添加”-----在可执行文件中加入

C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/aspnet_isapi.dll

扩展名:.js

动作:选择“限制”,输入GET,HEAD,POST,DEBUG

脚本引擎和检查文件是否存在,勾上

注意:配置IIS后,在web.config中不能出现下面的代码

<httpHandlers>
   <add verb="*" path="*.js" type="System.Web.HttpForbiddenHandler"/>
  </httpHandlers>

如果有则js文件不能访问。