省略号应用于文本时如何显示工具提示?

时间:2014-03-18 09:26:28

标签: javascript jquery

我尝试使用以下代码:

$('td').bind('mouseenter', function(){
    var $this = $(this);
    if(this.offsetWidth < this.scrollWidth){
        var text = $this.text();
        $this.attr("data-title",text);
        $this.tipper({
            direction: "bottom",
            follow: true
        });
    }
});

jsFiddle

工作正常,但不是第一次,有必要再次将鼠标移开以使其正常工作 任何帮助是极大的赞赏!谢谢!

PS。我不想插入data-title内联。

3 个答案:

答案 0 :(得分:6)

Fiddle Demo

$('td').bind('mouseenter', function () {
    var $this = $(this);
    if (this.offsetWidth < this.scrollWidth) {
        var text = $this.text();
        $this.attr("data-title", text);
        $this.tipper({
            direction: "bottom",
            follow: true
        });
    }
}).mouseenter();
// ^ trigger the mouseenter event so that tooltip get bind to td
  

注意:首先加载工具提示插件而不是编写此方法

<小时/> 更新您也可以

Fiddle Demo

$('td').each(function () {
    var $this = $(this);
    if (this.offsetWidth < this.scrollWidth) {
        var text = $this.text();
        $this.attr("data-title", text);
        $this.tipper({
            direction: "bottom",
            follow: true
        });
    }
});

答案 1 :(得分:1)

您没有正确使用API​​。 .tipper没有显示工具提示,它会绑定事件。请注意,这也有效。

var text = $('td').text();
$('td').attr("data-title",text);
$('td').tipper({
  direction: "bottom",
  follow: true
});

答案 2 :(得分:1)

请参阅此代码,了解是否有elipsis,然后出现工具提示

$(function() {
    $('div').each(function(i) {
        if (isEllipsisActive(this)) {
            $(this).css({'color':'red'});
            $(this).attr("title", $(this).text());
        }
        else
             $(this).css({'color':'green'});
    });
});

function isEllipsisActive(e) {
     return (e.offsetWidth < e.scrollWidth);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="div-cless" style="width:158px;text-overflow:ellipsis;overflow:hidden;white-space:nowrap;">neel upadhyay @g mail .com hello world
</div>
    
<div class="div-cless" style="width:158px;text-overflow:ellipsis;overflow:hidden;white-space:nowrap;">
    asdasf a
<div>

相关问题