<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JS控制TITLE懸停效果</title>
<script type="text/javascript">
function getClass(className,tagname) {
//tagname預設值為'*',不能直接寫成預設參數方式getClass(className,tagname='*'),否則IE下報錯
if(typeof tagname == 'undefined') tagname = '*';
if(typeof(getElementsByClassName) == 'function') {
return getElementsByClassName(className);
}else {
var tagname = document.getElementsByTagName(tagname);
var tagnameAll = [];
for(var i = 0; i < tagname.length; i++) {
if(tagname[i].className == className) {
tagnameAll[tagnameAll.length] = tagname[i];
}
}
return tagnameAll;
}
}
function split_str(string,words_per_line) {
//空串,直接傳回
if(typeof string == 'undefined' || string.length == 0) return '';
//單行字數未設定,非數值,則取預設值50
if(typeof words_per_line == 'undefined' || isNaN(words_per_line)){
words_per_line = 50;
}
//格式化成整形值
words_per_line = parseInt(words_per_line);
//取出i=0時的字,避免for循環裡換行時多次判斷i是否為0
var output_string = string.substring(0,1);
//循環分隔字元串
for(var i=1;i<string.length;i++) {
//如果目前字元是每行顯示的字元數的倍數,輸出換行
if(i%words_per_line == 0) {
output_string += "<br/>";
}
//每次拼入一個字元
output_string += string.substring(i,i+1);
}
return output_string;
}
function titleMouseOver(obj,event,words_per_line) {
//無TITLE懸停,直接傳回
if(typeof obj.title == 'undefined' || obj.title == '') return false;
//不存在title_show标簽則自動建立
var title_show = document.getElementById("title_show");
if(title_show == null){
title_show = document.createElement("div"); //建立Element
document.getElementsByTagName('body')[0].appendChild(title_show); //加入body中
var attr_id = document.createAttribute('id'); //建立Element的id屬性
attr_id.nodeValue = 'title_show'; //為id屬性指派
title_show.setAttributeNode(attr_id); //為Element設定id屬性
var attr_style = document.createAttribute('style'); //建立Element的style屬性
attr_style.nodeValue = 'position:absolute;' //絕對定位
+'border:solid 1px #999999; background:#EDEEF0;' //邊框、背景顔色
+'border-radius:2px;box-shadow:2px 3px #999999;' //圓角、陰影
+'line-height:18px;' //行間距
+'font-size:12px; padding: 2px 5px;'; //字型大小、内間距
try{
title_show.setAttributeNode(attr_style); //為Element設定style屬性
}catch(e){
//IE6
title_show.style.position = 'absolute';
title_show.style.border = 'solid 1px #999999';
title_show.style.background = '#EDEEF0';
title_show.style.lineHeight = '18px';
title_show.style.fontSize = '18px';
title_show.style.padding = '2px 5px';
}
}
//存儲并删除原TITLE
document.title_value = obj.title;
obj.title = '';
//單行字數未設定,非數值,則取預設值50
if(typeof words_per_line == 'undefined' || isNaN(words_per_line)){
words_per_line = 50;
}
//格式化成整形值
words_per_line = parseInt(words_per_line);
//在title_show中按每行限定字數顯示标題内容,模拟TITLE懸停效果
title_show.innerHTML = split_str(document.title_value,words_per_line);
//顯示懸停效果DIV
title_show.style.display = 'block';
//根據滑鼠位置設定懸停效果DIV位置
event = event || window.event; //滑鼠、鍵盤事件
var top_down = 15; //下移15px避免遮蓋目前标簽
//最左值為目前滑鼠位置 與 body寬度減去懸停效果DIV寬度的最小值,否則将右端導緻遮蓋
var left = Math.min(event.clientX,document.body.clientWidth-title_show.clientWidth);
title_show.style.left = left+"px"; //設定title_show在頁面中的X軸位置。
title_show.style.top = (event.clientY + top_down)+"px"; //設定title_show在頁面中的Y軸位置。
}
function titleMouseOut(obj) {
var title_show = document.getElementById("title_show");
//不存在懸停效果,直接傳回
if(title_show == null) return false;
//存在懸停效果,恢複原TITLE
obj.title = document.title_value;
//隐藏懸停效果DIV
title_show.style.display = "none";
}
function attachEvent(objs,words_per_line){
if(typeof objs != 'object') return false;
//單行字數未設定,非數值,則取預設值50
if(typeof words_per_line == 'undefined' || isNaN(words_per_line)){
words_per_line = 50;
}
for(i=0;i<objs.length;i++){
objs[i].onmouseover = function(event){
titleMouseOver(this,event,words_per_line);
}
objs[i].onmouseout = function(event){
titleMouseOut(this);
}
}
}
//初始化,當頁面onload的時候,對所有class="title_hover"的标簽綁定TITLE懸停事件
window.onload = function(){
attachEvent(getClass('title_hover'),18); //行字數設定為18
}
</script>
</head>
<body>
<style>
tr{float:left; margin:0 50px;}
</style>
<table>
<tr>
<td title="這個是預設的TITLE這個是預設的TITLE這個是預設的TITLE這個是預設的TITLE這個是預設的TITLE這個是預設的TITLE">滑鼠懸停[原生版本]</td>
</tr>
<tr>
<td title="這個是JS實作懸停的TITLE這個是JS實作懸停的TITLE這個是JS實作懸停的TITLE這個是JS實作懸停的TITLE這個是JS實作懸停的TITLE"
οnmοuseοver="titleMouseOver(this,event,15);" οnmοuseοut="titleMouseOut(this);">滑鼠懸停[直接調用函數版本,設定行字數]</td>
</tr>
<tr>
<td class="title_hover" title="ABCTesterABCTesterABCTesterABCTesterABCTesterABCTesterABCTester">滑鼠懸停[class控制版本]</td>
</tr>
<tr>
<td title="這個是JS實作懸停的TITLE這個是JS實作懸停的TITLE這個是JS實作懸停的TITLE這個是JS實作懸停的TITLE這個是JS實作懸停的TITLE"
οnmοuseοver="titleMouseOver(this,event);" οnmοuseοut="titleMouseOut(this);">滑鼠懸停[直接調用函數版本,預設行字數]</td>
</tr>
</table>
</body>
</html>