How can I access the actual text that is displayed in a DIV when using CSS style overflow: hidden?

馋奶兔 提交于 2019-11-30 09:19:52

问题


I have the following content DIV on my page, which displays dynamic text:

<div id="someContent">
</div>

It uses the following CSS to cut off additional text:

#someContent {
    height: 200px;
    width: 200px;
    overflow: hidden;
}

If I load this text into the DIV:

"Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Praesent aliquam, justo convallis luctus rutrum, erat nulla fermentum diam, at nonummy quam ante ac quam. Maecenas urna purus, fermentum id, molestie in, commodo porttitor, felis. Nam blandit quam ut lacus. Quisque ornare risus quis ligula. Phasellus tristique purus a augue condimentum adipiscing. Aenean sagittis. Etiam leo pede, rhoncus venenatis, tristique in, vulputate at, odio. Donec et ipsum et sapien vehicula nonummy. Suspendisse potenti."

...the CSS causes only the following text to display in the DIV:

"Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Praesent aliquam, justo convallis luctus rutrum, erat nulla fermentum diam, at nonummy quam ante ac quam. Maecenas urna purus, fermentum id, molestie in, commodo porttitor, felis. Nam blandit quam ut lacus. Quisque ornare risus quis ligula. Phasellus tristique purus a augue"

This works as expected.

However, I was wondering if there is some way that I can access the displayed text using JavaScript. When I try to access the innerHTML property of the DIV, it returns the entire text originally loaded into the DIV.

<script type="text/javascript">
    alert(document.getElementById("mainArticleContent").innerHTML);
</script>

My end goal is to replace the last word in the trimmed content with ellipsis ("..."). I figured I could do this in JavaScript so that it will display in all browsers, not just IE, as with CSS property text-overflow: hidden.

Any ideas? Is this possible?


回答1:


I don't think what you're asking to do is possible with javascript.

Alternatively, why not truncate the text at a specific character count server-side? You probably won't have the kind of control you want over the size of the div displayed on the page depending on the font you've selected but it should solve your problem.

Edit:

Take a look at this to get automatic CSS ellipsis. There are a few caveats and it looks pretty browser-specific but this is another method of doing what you want.




回答2:


This site explains a technique that may be useful for getting the displayed portion of text. I'm not sure how accurate this method would be, but I suspect it'd get you pretty close to what you want: http://www.ruzee.com/blog/2007/08/ellipsis-or-truncate-with-dots-via-javascript




回答3:


There's no easy way to do it. You have to calculate how much text can be visible in the DIV based on div size, font size, font type and text offset inside div. Remember you can use negative offset to show different parts from the text.

You should be able to figure out the algorythm to calculate how much of your text the DIV can hold based on all of those numbers, but it's a bit tricky because of linewrap.




回答4:


Here's a quick thought using jQuery:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
    var innerText = $('#someContent').text();
    var textArray = innerText.split('');
    var formatted = '';
    for(var i in textArray) {
        formatted += '<span>' + textArray[i] + '</span>';
    }
    var heightOfContainer = $('#someContent').height();
    $('#someContent').html(formatted);
    var clipped = '';
    $('#someContent span').each(function(){
        if ($(this).position().top < heightOfContainer) {
            clipped += $(this).text();
        } else {
            return false;
        }
    });
    clipped += '...';
    $('#someContent').html(clipped);
});
</script>



回答5:


There is no need to use Javascript to have the ellipsis. You can simply use the CSS text-overflow property and set its value to "ellipsis". You see the documentation here.

And if you want to have it after multiple lines, then advise this guide.




回答6:


You could do the same truncation based on character count on the client side. Store the truncated part in some other DOM element (or your own attribute if you don't mind that sort of thing). Have the whole thing set to kick off on body.onLoad().




回答7:


If this is simply for display purposes then you could simply have an absolutely positioned div with "..." in it at the bottom-right of the container, so over the top of the text.



来源:https://stackoverflow.com/questions/777340/how-can-i-access-the-actual-text-that-is-displayed-in-a-div-when-using-css-style

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!