[JavaScript]自定义Title的显示方式(zt)

好久没有写blog了,今天更新下最近在做的事情,顺便让朋友们了解下, 我还顽强地活着 05.gif...


最近刚做完公司的WEB MVP 开发框架.累得脑子快要抽筋了,可是即使要抽筋了,也得继续上啊...谁叫俺是打工仔呢.

现在研究的重点转移到了客户端,于是开始钻研起dojo,prototype,yui......

发现javascript也在OO呢(虽然只是同vb一样基于对象的)~~~呵呵

以前只会找要的js代码,然后改啊改啊...从来没有认真的学习过,这次决定好好学学这个Web开发必不可少的东东...

好了废话不扯了,这次的任务就是做Title的自定义显示.

我想IE的Tip显示方式,肯定大家都不满意吧.每一天有多少人在做着自己的自定义显示.

俺可没有那么多精力,先是从网络上找找到了一个老外的程序 qTip: http://solardreamstudios.com/_img/learn/css/qtip/qTip.html

仔细看了看代码,发现几个问题:
1.这个老外只做了一半,它只对单个Tag的所有元素集合进行自定义显示
1 None.gif var  qTipTag  =   " a " // Which tag do you want to qTip-ize? Keep it lowercase!//

2.循环出来每个挂载mouse事件
1 None.gif var  anchors  =  document.getElementsByTagName (qTipTag);
2 None.gif
3 ExpandedBlockStart.gifContractedBlock.gif     for  ( var  i  =   0 ; i  <  anchors.length; i  ++ dot.gif {
4InBlock.gif//dot.gif.
5ExpandedBlockEnd.gif}


3.Event采用赋值形式
1 ExpandedBlockStart.gif ContractedBlock.gif     a.onmouseover  =   function ()  dot.gif {tooltip.show(this.getAttribute('tiptitle'))} ;
2 ExpandedBlockStart.gifContractedBlock.gif            a.onmouseout  =   function ()  dot.gif {tooltip.hide()} ;
3 None.gif

OK.针对这些问题,我们开始我们的重构之旅

1.首先,我希望页面控件的所有Tag只要有Title的,我都要采用自定义显示,这时想到一个最土的招就是
None.gif var  anchors  =  document.all;

2.它的实现方式是循环找到所有的有标记的Tag,然后设置mouse事件.
  这一步我觉得有2个问题:
  a.每个tag都设置mouse的事件,显然页面变大了,特别是我现在要求所有的Tag都要自定义显示
  b.Event采用赋值.......寒一个

  我的思路是使用事件的串连机制,把mouse的事件直接挂载到document上面,然后进行冒泡处理(我不知道这样的效率实际上是更好呢,还是更坏,自我感觉应该会好些吧?:))

 
 1 None.gif document.attachEvent( " onmouseover " , function (e)
 2 ExpandedBlockStart.gifContractedBlock.gif         dot.gif {            
 3ExpandedSubBlockStart.gifContractedSubBlock.gif            if(typeof(event)=="undefined")dot.gif{
 4InBlock.gif                sTitle = e.target.getAttribute("title")
 5InBlock.gif                e.target.removeAttribute("title");
 6ExpandedSubBlockStart.gifContractedSubBlock.gif            }
elsedot.gif{
 7InBlock.gif                e    = event;
 8InBlock.gif                sTitle = e.srcElement.title;
 9InBlock.gif                e.srcElement.title = "";
10ExpandedSubBlockEnd.gif            }
;
11InBlock.gif    
12InBlock.gif            if(!sTitle == "")
13ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{        
14InBlock.gif                tooltip.show(sTitle);
15ExpandedSubBlockEnd.gif            }

16ExpandedBlockEnd.gif        }

17 None.gif    );
18 None.gif
19 None.gif    document.attachEvent('onmouseout', function (e)
20 ExpandedBlockStart.gifContractedBlock.gif         dot.gif {
21InBlock.gif
22ExpandedSubBlockStart.gifContractedSubBlock.gif            if(typeof(event)=="undefined")dot.gif{
23InBlock.gif                e.target.setAttribute("title",sTitle);
24ExpandedSubBlockStart.gifContractedSubBlock.gif            }
elsedot.gif{
25InBlock.gif                e    = event;
26InBlock.gif                e.srcElement.title = sTitle;
27ExpandedSubBlockEnd.gif            }
;
28InBlock.gif            
29InBlock.gif            tooltip.hide();
30InBlock.gif        
31ExpandedBlockEnd.gif        }

32 None.gif    );
33 None.gif

当然这里或许会碰到的所谓的IE 内存泄漏,因为没有detachEvent.(本来想使用Event.observer的,不过prototype还不谈熟,加上脑子也晕乎乎了,留待下次重构吧)...

None.gif document.attachEvent 

看来好像只支持IE哦....还是对少数的其他Fans支持一下吧,修改一下系统方法,让它适当兼容一下其他浏览器
 1 None.gif if ( ! document.attachEvent) // Not IE
 2 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 3ExpandedSubBlockStart.gifContractedSubBlock.gif    document.attachEvent = function()dot.gif{document.addEventListener(arguments[0].substr(2),arguments[1],arguments[2])}
 4ExpandedBlockEnd.gif}

 5 None.gif
 6 None.gif if ( ! window.attachEvent) // Not IE
 7 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 8ExpandedSubBlockStart.gifContractedSubBlock.gif    window.attachEvent = function()dot.gif{window.addEventListener(arguments[0].substr(2),arguments[1],arguments[2])}
 9ExpandedBlockEnd.gif}

10 None.gif


这样子,大概qTip的功能就重构结束了...

当然还有很多可以改进的地方,比如把提示用的层设计得漂亮一点...

ExpandedBlockStart.gif ContractedBlock.gif      if ( ! tipContainer)  dot.gif {
InBlock.gif      tipContainer 
= document.createElementNS ? document.createElementNS(tipNameSpaceURI, "div") : document.createElement("div");
InBlock.gif        tipContainer.setAttribute(
"id", tipContainerID);
InBlock.gif
//美化下?
InBlock.gif
      document.getElementsByTagName("body").item(0).appendChild(tipContainer);
ExpandedBlockEnd.gif    }

None.gif


重构后的源文件(在需要的页面link这2个文件就好了):

ContractedBlock.gif ExpandedBlockStart.gif qTip.js
  1None.gif//偷抄的^_^
  2ExpandedBlockStart.gifContractedBlock.giffunction $() dot.gif{
  3InBlock.gif  var elements = new Array();
  4InBlock.gif
  5ExpandedSubBlockStart.gifContractedSubBlock.gif  for (var i = 0; i < arguments.length; i++dot.gif{
  6InBlock.gif    var element = arguments[i];
  7InBlock.gif    if (typeof element == 'string')
  8InBlock.gif      element = document.getElementById(element);
  9InBlock.gif
 10InBlock.gif    if (arguments.length == 1)
 11InBlock.gif      return element;
 12InBlock.gif
 13InBlock.gif    elements.push(element);
 14ExpandedSubBlockEnd.gif  }

 15InBlock.gif
 16InBlock.gif  return elements;
 17ExpandedBlockEnd.gif}

 18None.gif
 19None.gif//兼容性
 20None.gifif(!document.attachEvent)//Not IE
 21ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 22ExpandedSubBlockStart.gifContractedSubBlock.gif    document.attachEvent = function()dot.gif{document.addEventListener(arguments[0].substr(2),arguments[1],arguments[2])}
 23ExpandedBlockEnd.gif}

 24None.gif
 25None.gifif(!window.attachEvent)//Not IE
 26ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 27ExpandedSubBlockStart.gifContractedSubBlock.gif    window.attachEvent = function()dot.gif{window.addEventListener(arguments[0].substr(2),arguments[1],arguments[2])}
 28ExpandedBlockEnd.gif}

 29None.gif
 30None.gif//位置
 31None.gifvar qTipX = -30//This is qTip's X offset//
 32None.gifvar qTipY = 25//This is qTip's Y offset//
 33None.gif
 34ExpandedBlockStart.gifContractedBlock.giftooltip = dot.gif{
 35InBlock.gif  name : "qTip",
 36InBlock.gif  offsetX : qTipX,
 37InBlock.gif  offsetY : qTipY,
 38InBlock.gif  tip : null
 39ExpandedBlockEnd.gif}

 40None.gif
 41ExpandedBlockStart.gifContractedBlock.giftooltip.init = function () dot.gif{
 42InBlock.gif    var tipNameSpaceURI = "http://www.w3.org/1999/xhtml";
 43ExpandedSubBlockStart.gifContractedSubBlock.gif    if(!tipContainerID)dot.gifvar tipContainerID = "qTip";}
 44InBlock.gif    var tipContainer = document.getElementById(tipContainerID);
 45InBlock.gif
 46ExpandedSubBlockStart.gifContractedSubBlock.gif    if(!tipContainer) dot.gif{
 47InBlock.gif      tipContainer = document.createElementNS ? document.createElementNS(tipNameSpaceURI, "div") : document.createElement("div");
 48InBlock.gif        tipContainer.setAttribute("id", tipContainerID);
 49InBlock.gif      document.getElementsByTagName("body").item(0).appendChild(tipContainer);
 50ExpandedSubBlockEnd.gif    }

 51InBlock.gif
 52InBlock.gif    this.tip = $(this.name);
 53InBlock.gif    if (this.tip) 
 54ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 55ExpandedSubBlockStart.gifContractedSubBlock.gif        document.attachEvent("onmousemove",function (e) dot.gif{tooltip.move (e)});
 56ExpandedSubBlockEnd.gif    }

 57InBlock.gif    var sTitle;
 58InBlock.gif    
 59InBlock.gif        //串连onmouseover事件
 60InBlock.gif    document.attachEvent("onmouseover",function(e)
 61ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{            
 62ExpandedSubBlockStart.gifContractedSubBlock.gif            if(typeof(event)=="undefined")dot.gif{
 63InBlock.gif                sTitle = e.target.getAttribute("title")
 64InBlock.gif                e.target.removeAttribute("title");
 65ExpandedSubBlockStart.gifContractedSubBlock.gif            }
elsedot.gif{
 66InBlock.gif                e    = event;
 67InBlock.gif                sTitle = e.srcElement.title;
 68InBlock.gif                e.srcElement.title = "";
 69ExpandedSubBlockEnd.gif            }
;
 70InBlock.gif    
 71InBlock.gif            if(!sTitle == "")
 72ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{        
 73InBlock.gif                tooltip.show(sTitle);
 74ExpandedSubBlockEnd.gif            }

 75ExpandedSubBlockEnd.gif        }

 76InBlock.gif    );
 77InBlock.gif
 78InBlock.gif        //串连onmouseout事件
 79InBlock.gif    document.attachEvent('onmouseout',function(e)
 80ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 81InBlock.gif
 82ExpandedSubBlockStart.gifContractedSubBlock.gif            if(typeof(event)=="undefined")dot.gif{
 83InBlock.gif                e.target.setAttribute("title",sTitle);
 84ExpandedSubBlockStart.gifContractedSubBlock.gif            }
elsedot.gif{
 85InBlock.gif                e    = event;
 86InBlock.gif                e.srcElement.title = sTitle;
 87ExpandedSubBlockEnd.gif            }
;
 88InBlock.gif            
 89InBlock.gif            tooltip.hide();
 90InBlock.gif        
 91ExpandedSubBlockEnd.gif        }

 92InBlock.gif    );
 93ExpandedBlockEnd.gif}

 94None.gif
 95None.gif//移动事件
 96ExpandedBlockStart.gifContractedBlock.giftooltip.move = function (evt) dot.gif{
 97InBlock.gif//Get mouse X & Y
 98InBlock.gif    var x=0, y=0;
 99ExpandedSubBlockStart.gifContractedSubBlock.gif    if (document.all) dot.gif{//IE
100InBlock.gif        x = (document.documentElement && document.documentElement.scrollLeft) ? document.documentElement.scrollLeft : document.body.scrollLeft;
101InBlock.gif        y = (document.documentElement && document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop;
102InBlock.gif        x += window.event.clientX;
103InBlock.gif        y += window.event.clientY;
104InBlock.gif        
105ExpandedSubBlockStart.gifContractedSubBlock.gif    }
 else dot.gif{//Good Browsers
106InBlock.gif        x = evt.pageX;
107InBlock.gif        y = evt.pageY;
108ExpandedSubBlockEnd.gif    }

109InBlock.gif    this.tip.style.left = (x + this.offsetX) + "px";
110InBlock.gif    this.tip.style.top = (y + this.offsetY) + "px";
111ExpandedBlockEnd.gif}

112None.gif
113ExpandedBlockStart.gifContractedBlock.giftooltip.show = function (text) dot.gif{
114InBlock.gif    if (!this.tip) return;
115InBlock.gif    this.tip.innerHTML = text;
116InBlock.gif    this.tip.style.display = "block";
117ExpandedBlockEnd.gif}

118None.gif
119ExpandedBlockStart.gifContractedBlock.giftooltip.hide = function () dot.gif{
120InBlock.gif    if (!this.tip) return;
121InBlock.gif    this.tip.innerHTML = "";
122InBlock.gif    this.tip.style.display = "none";
123ExpandedBlockEnd.gif}

124None.gif
125None.gif//load的时候初始化自定义显示方式
126ExpandedBlockStart.gifContractedBlock.gifwindow.attachEvent("onload",function(e)dot.gif{tooltip.init();});
127None.gif
128None.gif
129None.gif
130None.gif
131None.gif
132None.gif

 

ContractedBlock.gif ExpandedBlockStart.gif qTip.css
 1ExpandedBlockStart.gifContractedBlock.gifdiv#qTip {dot.gif}{
 2InBlock.gif  padding: 3px;
 3InBlock.gif  border: 1px solid gray;
 4InBlock.gif  border-right-width: 2px;
 5InBlock.gif  border-bottom-width: 2px;
 6InBlock.gif  display: none;
 7InBlock.gif  background: #999;
 8InBlock.gif  background-color:#ffffff;
 9InBlock.gif  color: #349045;
10InBlock.gif  font-size:9pt;
11InBlock.gif  font: bold 9px Verdana, Arial, Helvetica, sans-serif;
12InBlock.gif  text-align: left;
13InBlock.gif  position: absolute;
14InBlock.gif  z-index: 1000;
15InBlock.gif  padding:"5px 8px 3px 8px"; 
16ExpandedBlockEnd.gif}

17None.gif

javascript控制TEXTBOX长度
< SCRIPT  language =javascript1.3 > dot.gif <!--
InBlock.gif
var testleng=300;//允许多少字符串填入
InBlock.gif

ExpandedSubBlockStart.gifContractedSubBlock.gif
function GC_UpdateCharCount() dot.gif{
InBlock.gif  
var desc_el = document.getElementById('desc');
InBlock.gif  
var remainingchars_el = document.getElementById('remainingchars');
InBlock.gif
InBlock.gif  
var num_remaining = testleng - desc_el.value.length;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif  
if (num_remaining >= 0dot.gif{
InBlock.gif  remainingchars_el.innerHTML 
= num_remaining;
ExpandedSubBlockStart.gifContractedSubBlock.gif  }
 else dot.gif{
InBlock.gif  remainingchars_el.innerHTML 
= "<font color=red>" +
InBlock.gif  (
-num_remaining + '') + " characters over</font>";
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockEnd.gif}

InBlock.gif
function isOver(sText)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
var intlen=sText.value.length;
InBlock.gif                
if (intlen>testleng)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    alert(
"描述特征的内容的字数必修小于或者等于 "+testleng);
InBlock.gif                    sText.focus();
InBlock.gif                    sText.select();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif
//--></SCRIPT>
InBlock.gif
</head>
InBlock.gif
InBlock.gif
<body>
InBlock.gif
<<B>Group description</B><BR><TEXTAREA id=desc onkeydown=GC_UpdateCharCount() onkeyup=GC_UpdateCharCount()  onblur="isOver(this);"style="WIDTH: 99%" name=desc rows=4 cols=50></TEXTAREA> 
InBlock.gif            
<BR>Letters remaining: <SPAN id=remainingchars>
InBlock.gif            
<SCRIPT language=javascript1.2><!--
InBlock.gif              GC_UpdateCharCount();
InBlock.gif              
//--></SCRIPT></SPAN><NOSCRIPT>300 characters allowed </NOSCRIPT>
InBlock.gif
</body>
InBlock.gif
</html>
InBlock.gif

转载于:https://www.cnblogs.com/nosnowwolf/articles/467380.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值