How to determine if shortening is applied in my style through JS?

I use truncation using CSS styles:

.yui-skin-sam td:not(.yui-dt-editable) .yui-dt-liner{
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    -ms-text-overflow: ellipsis;        
    -o-text-overflow: ellipsis;
    -moz-binding: url('ellipsis.xml#ellipsis');     
}

.yui-skin-sam td[class~=yui-dt-editable] .yui-dt-liner{
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    -ms-text-overflow: ellipsis;        
    -o-text-overflow: ellipsis;
}

(Sidenote: I'm not sure if this is the best way to write my CSS. This is a malicious approach for Firefox since truncating on Firefox only sorts the work).

I want the tooltip to appear above the text that is truncated. How to determine if the text is shortened so that I can display a tooltip?

+3
source share
2 answers

Keep in mind that CSS never changes the DOM !!!

jQuery snippets:

$(function() {
    $('a.link').each(function(e) {
        var link = $(this).text();
        if ( link.length > 100) {
            $(this).attr('title', link );
        }
    });
});

Assuming you have links

<a class="link" href="" >the brown fox jumps over the lazy dog</a>

the wil code above produces

    <a class="link" href="" title="the brown fox jumps over the lazy dog" >
     the brown fox jumps over the lazy dog
    </a>

the property text-overflow: ellipsis;will do the rest, as you know!


GOES FORWARD:

there is a small plugin

, jQuery Firefox. , ellipsis() jQuery. :

$("span").ellipsis();
+1

.

, @aSeptik, , jQuery.

, : clientWith scrollWidth.

, elem.scrollWidth > elem.clientWidth.

MDN: https://developer.mozilla.org/en-US/docs/Web/API/element.scrollWidth

jsFiddle: http://jsfiddle.net/diegof79/de9xsn4b/

+9

Source: https://habr.com/ru/post/1747671/


All Articles