canrun

 <!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">
/**
* className 类名
* tagname HTML标签名,如div,td,ul等
* @return Array 所有class对应标签对象组成的数组
* @example
<div class="abc">abc</div>
var abc = getClass('abc');
for(i=0;i<abc.length;i++){
abc[i].style.backgroundColor='red';
}
*/
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;
}
} /**
* JS字符切割函数
* @params string 原字符串
* @params words_per_line 每行显示的字符数
*/
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;
} /**
* 鼠标悬停显示TITLE
* @params obj 当前悬停的标签
*
*/
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轴位置。
}
/**
* 鼠标离开隐藏TITLE
* @params obj 当前悬停的标签
*
*/
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";
} /**
* 悬停事件绑定
* @params objs 所有需要绑定事件的Element
*
*/
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"
onmouseover="titleMouseOver(this,event,15);" onmouseout="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"
onmouseover="titleMouseOver(this,event);" onmouseout="titleMouseOut(this);">鼠标悬停[直接调用函数版本,默认行字数]</td>
</tr>
</table>
</body>
</html>

JS实现TITLE悬停长久显示效果的更多相关文章

  1. js处理title超长问题

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  2. js设置鼠标悬停改变背景色

    看了网上那么多的js鼠标悬停时事件,大多数的,说了那么多话,也没解决什么问题,现在直接贴上代码,以供参考 html: <div id="sign">this is te ...

  3. CSS或者JS实现鼠标悬停显示另一元素

    想达到鼠标悬停到元素a上,显示另一个元素b,可以通过css实现也可以通过js实现.js:写两个函数:mouseenter,mouseleave,例如:其中 $("#a").mous ...

  4. [javascript]js修改title

    使用javascript修改title 1.这个在chrome中可以成功,在ie8中报错 <!DOCTYPE html> <html> <head> <tit ...

  5. jquery.imagezoom.js制作鼠标悬停图片放大镜特效、参数和最简教程

    一.插件介绍 今天在用到放大镜效果的时候,突然发现网站里没有放大镜的插件.于是总结了一下,放到这里.为自己,也为他人提供方便.jquery.imagezoom.js这款插件用途很简单,就是鼠标移过去, ...

  6. js修改title

    document.title = 'xxxxxx';

  7. js 根据title从下级往上级查找

    var menuData = [{ name: 'manage', title: '测试1', icon: 'home', }, { title: '测试2', name: 'car-parent', ...

  8. js的title提示

    $(function() { //先在页面创建一个层 var jqtip = $("<div id='jqtip20130719'" + "style='paddi ...

  9. vue title悬停

    title 设置name="peo" title="" v-on:mouseenter="peoAndCarHover(item.signStatus ...

随机推荐

  1. 学习WPF——初识依赖项属性

    入门 首先创建一个依赖项属性 然后绑定父容器的DataContext到这个依赖项的实例 接着绑定子元素的属性到依赖项属性(注意Button的Content属性) 程序最终的运行结果:   说明 首先是 ...

  2. 实验五 含有控制信号的计数器VHDL设计

    一.实验目的 学习计数器的设计.仿真和硬件测试,进一步熟悉VHDL设计技术. 二.实验仪器与器材 计算机1台,GW48-PK2S实验箱1台,QuartusⅡ6.0 1套. 三.实验 1. 基本命题 在 ...

  3. 通过Anuglar Material串串学客户端开发 - NodeJS模块机制之Module.Exports

    module.exports 前文讲到在Angular Material的第二个编译文件docs/gulpfile.js中却看到了一个奇怪的东西module.exports那么module.expor ...

  4. [游戏模版16] Win32 飞机射击 敌人追踪

    >_<:AI introduction. >_<:According the plane position (nowX,nowY) relative to birds' pos ...

  5. JS原型链简单图解

    JS中原型链,说简单也简单. 首先明确: 函数(Function)才有prototype属性,对象(除Object)拥有__proto__. 首先,我画了一张图. 所谓原型链,指的就是图中的proto ...

  6. Atitit.实现继承的原理and方法java javascript .net c# php ...

    Atitit.实现继承的原理and方法java javascript .net c# php ... 1. 实现继承的问题 1 2. 如何拷贝基类方法?采用prototype原型方式,通过冒充对象 1 ...

  7. atitit. js 跨界面 页面 web cs 传值方法总结

    atitit. js 跨界面 页面 web cs 传值方法总结 #--需求 js #---两个方法:   直接传跟跟间接传递... 1.直接传跟new form(param)    web使用url方 ...

  8. iOS开发——高级技术&地图功能的实现

    地图功能的实现 因为有个项目要在地图中显示位置,所以用到了MapKit. 记录下来,以免以后忘记. 加入MapKit library 首先得在项目中加入MapKit,如图 MapView 先增加一个V ...

  9. VS 2008 创建MFC程序对话框的步骤

    用过不少编程语言,可是刚开始学的时候最容易忘记一些简单的流程或者生疏了.那么这里就说说VS 2008 创建MFC程序对话框的步骤.我主要是android开发方面的.平时使用jni调用不少c++代码.所 ...

  10. Jsp技术总结

    这节我们总结一下Jsp的相关技术. 1. 什么是JSP JSP即Java Server Pages,它和servlet技术一样,都是sun公司定义的一种用于开发动态web资源的技术.该技术的最大特点在 ...