转自网络,可直接copy使用。
在实际的开发过程中,如过不想新出明细的页面,就可以用title的属性把明细信息都让鼠标悬浮的时候显示出来,但title属性显示出来可能会影响本来页面的信息展示(覆盖原有信息),请结合实际的场景进行使用。
代码如下:


<!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=gbk">
<title>JS控制TITLE悬停效果 - veryhuo.com</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);
}
}
}
// Downloads By http://www.veryhuo.com
//初始化,当页面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><div style="text-align:center;margin:30px 0 0 0;"><hr style="color:#999;height:1px;">如不能显示效果,请按Ctrl+F5刷新本页。</div>

最后效果图:

自定义html 标签中的 title属性,感觉默认title 难看,强迫症专用相关推荐

  1. [html] html标签中的lang属性有什么作用?

    [html] html标签中的lang属性有什么作用? 根据lang属性来设定不同语言的css样式,或者字体告诉搜索引擎做精确的识别让语法检查程序做语言识别帮助翻译工具做识别帮助网页阅读程序做识别等等 ...

  2. script标签中的crossorigin属性

    在前端监控逐渐完善的今天,页面中错误日志的上报可以说对我们的日常工作带来了极大的帮助. 而使用 window.onerror 事件来捕获 js 脚本中的错误信息是重要的手段 . 但是对于跨域的资源 , ...

  3. html标签中的lang属性

    写在html标签中的lang属性作用:声明当前页面的语言类型. 如: <html lang='en'></html> //英文 <html lang='zh'>&l ...

  4. [暑假]<script>标签中的type属性详解

    type属性是必须写的属性吗? 不写的情况: <script>标签中的type属性不是必须写的属性, 因为按照惯例,这个值始终都是"text/javascript", ...

  5. c:out标签中的escapeXML属性

    <c:out>标签中的escapeXML属性 在<c:out>中,escapeXML属性默认为true. 当设置escapeXML的属性为true时,将value中的值以字符串 ...

  6. java escapexml_lt;c:outgt;标签中的escapeXml属性 - Jack Stomtion - ITeye博客

    最近在做一个在线文本编辑器项目,遇到一个问题: 我从后台得到了各个章节的信息,包括标题和内容,其中内容是以HTML格式的形式存储的.这些章节信息是以一个List返回的.在页面中的显示代码如下: &qu ...

  7. cout标签中的escapeXML属性

    <c:out>标签中的escapeXML属性 在<c:out>中,escapeXML属性默认为true. 当设置escapeXML的属性为true时,将value中的值以字符串 ...

  8. html.textboxfor属性,label标签中的for属性与form属性

    HTML中的label标签是干什么的?在讲这个标签之前先来做一个示范,请点击以下的文本框控件: 你的姓名是: 大家都知道上面的文本框使用的是input元素,当鼠标点击文本框时就能输入文本,若是点击文本 ...

  9. label标签中的for属性

    1. 使用介绍 label中的for属性规定了label与哪个表单元素绑定.for属性的值和表单元素的id值一样,即可完成该label标签与该表单元素的绑定. <label for=" ...

最新文章

  1. OC字符串转C语言字符串
  2. As3.0与java数据类型的比较总结
  3. 每日一题(45)—— 字符数组找错
  4. 螺旋方阵(Leetcode第59题)
  5. 简单粗暴 我再送一波教程资料,Vue、大数据、AI都有
  6. 【java基础知识】修改字符串的编码格式
  7. java线程基础_Java多线程基础
  8. RuntimeError: expected a Variable argument, but got torch.FloatTensor
  9. 根据微信的公众号获取公众号的二维码 根据公众号获得二维码的图片
  10. 支付宝玉伯:我心目中的优秀API
  11. PHP-Smarty
  12. 线性规划中的灵敏度分析
  13. java大作业私人管家系统_微软蓝天云平台:中小企业的私人管家
  14. diybox路由器设置教程_图文教程:家用无线路由器设置 | 192路由网
  15. 循环队列求元素个数为什么为(rear-front+maxSize)%maxSize?
  16. Linux Qt cannot find -lGL错误完美解决方案
  17. 工作中遇到的一个问题:
  18. 包装设计模式-Decorator
  19. excel 重复方差分析_如何在Excel中运行方差方差分析的两种方法
  20. mysql导入文件出现Data truncated for column 'xxx' at row 1的原因

热门文章

  1. 从baostock获取5分钟k线的具体用例
  2. 犀牛Rhino:将点投影到网格(函数方法)
  3. 华为防火墙-7-dhcp
  4. PBAC相对于传统ABAC的优势
  5. html——表格统计
  6. windows 多线程(五) 互斥量(Mutex)
  7. python常见加密算法
  8. 【智能路由器】openwrt工具uci使用指南
  9. tkinter库详解
  10. 微信公众号原创检验,了解10个审核机制!