简体   繁体   English

在悬停的DIV中显示隐藏的文本

[英]Show Hidden Text in DIV on Hover

The desired effect is to have hidden text within a table cell show the full text across the cell when user hovers the mouse cursor over the text. 理想的效果是当用户将鼠标指针悬停在文本上方时,使表格单元格中的隐藏文本在该单元格中显示全文。 See link to problem diagram to see what I mean. 请参阅问题图链接以了解我的意思。 The "solution" denotes the desired effect while "problem" denotes what the code is currently doing. “解决方案”表示所需的效果,而“问题”表示代码当前正在执行的操作。 Much appreciated for the assistance. 非常感谢您的协助。

问题图

var jobSourceID = 'jobSource' + jobIndex;
jobSource.innerHTML = '<div id="' + jobSourceID + '">' + jobSourceValue + '</div>';
document.getElementById(jobSourceID).style.marginLeft = '3px';
document.getElementById(jobSourceID).style.maxWidth = '55px';
document.getElementById(jobSourceID).style.overflow = 'hidden';
document.getElementById(jobSourceID).style.textOverflow = 'ellipsis';
document.getElementById(jobSourceID).style.whiteSpace = 'nowrap';
document.getElementById(jobSourceID).addEventListener('mouseover', function () {
    document.getElementById(jobSourceID).style.overflow = 'visible';
    document.getElementById(jobSourceID).style.backgroundColor = '#555555';
});
document.getElementById(jobSourceID).addEventListener('mouseout', function () {
    document.getElementById(jobSourceID).style.overflow = 'hidden';
    document.getElementById(jobSourceID).style.backgroundColor = '';
});

You can do it like this: 您可以这样做:

 *{ padding:0; margin: 0; } td{ overflow:hidden; } td div{ background-color: #ccc; width:50px; } td:hover{ overflow: inherit } td:hover div{ width:100%; } 
 <table> <tr> <td> <div> asadgshfdadgfhgdgjgsfgfadsfahgdafgerre </div> </td> </tr> </table> 

You can set 'jobSourceID' width as auto on mouseover function. 您可以将“ jobSourceID”宽度设置为鼠标悬停时的自动功能。

document.getElementById(jobSourceID).style.width = "auto"; document.getElementById(jobSourceID).style.width =“ auto”;

If your problem ist just the styling of the background, you can style a SPAN inside the DIV and hide this instead of only the text. 如果您的问题仅是背景样式,则可以在DIV中设置SPAN样式并隐藏它而不是仅隐藏文本。

document.body.innerHTML = '<div id="' + jobSourceID + '"><span>' + jobSourceValue + '<span></div>';
document.getElementById(jobSourceID).style.marginLeft = '3px';
document.getElementById(jobSourceID).style.maxWidth = '55px';
document.getElementById(jobSourceID).style.overflow = 'hidden';
document.getElementById(jobSourceID).style.textOverflow = 'ellipsis';
document.getElementById(jobSourceID).style.whiteSpace = 'nowrap';
document.getElementById(jobSourceID).addEventListener('mouseover', function () {
    document.getElementById(jobSourceID).style.overflow = 'visible';
    document.getElementById(jobSourceID).children[0].style.backgroundColor = '#555555';
});
document.getElementById(jobSourceID).addEventListener('mouseout', function () {
    document.getElementById(jobSourceID).style.overflow = 'hidden';
    document.getElementById(jobSourceID).children[0].style.backgroundColor = '';
});

After trying all the suggested solutions posted, I see that none of them solves the problem I described in my post diagram. 在尝试了所有建议的解决方案之后,我发现它们都无法解决我在邮寄图中描述的问题。 However, I did some more searching and after googling "html tooltips" I found some good articles explaining the concept such that tooltips was the solution I needed. 但是,我进行了更多搜索,在搜索了“ html工具提示”之后,我发现了一些不错的文章来解释这一概念,这样工具提示才是我需要的解决方案。 So I managed to get this problem fixed on my own to my satisfaction. 因此,我设法自己解决了这个问题,使我感到满意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM