Вопрос по javascript – JavaScript - видимый текст DIV

13
----------------------------------------------------
| This is my text inside a div and I want the overf|low of the text to be cut
----------------------------------------------------

Обратите внимание, что я хочу, чтобы переполнение было удалено, поэтому свойство многоточия CSS не будет работать для меня. В общем, я хочу, чтобы текст выше выглядел так:

----------------------------------------------------
| This is my text inside a div and I want the overf|
----------------------------------------------------

Error: User Rate Limit Exceeded

EDIT

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded Savas Vedova
Error: User Rate Limit Exceeded Savas Vedova
Error: User Rate Limit Exceeded Jirka Kopřiva
Error: User Rate Limit Exceeded Jirka Kopřiva

Ваш Ответ

5   ответов
3

function count_visible(el){
    var padding, em, numc;
    var text = el.firstChild.data;
    var max = el.clientWidth;

    var tmp = document.createElement('span');
    var node = document.createTextNode();
    tmp.appendChild(node);
    document.body.appendChild(tmp);

    if(getComputedStyle)
        tmp.style.cssText = getComputedStyle(el, null).cssText;
    else if(el.currentStyle)
        tmp.style.cssText = el.currentStyle.cssText;

    tmp.style.position = 'absolute';
    tmp.style.overflow = 'visible';
    tmp.style.width = 'auto';

    // Estimate number of characters that can fit.
    padding = tmp.style.padding;
    tmp.style.padding = '0';
    tmp.innerHTML = 'M';
    el.parentNode.appendChild(tmp);
    em = tmp.clientWidth;
    tmp.style.padding = padding;      
    numc = Math.floor(max/em);

    var width = tmp.clientWidth;

    // Only one of the following loops will iterate more than one time
    // Depending on if we overestimated or underestimated.

    // Add characters until we reach overflow width
    while(width < max && numc <= text.length){
        node.nodeValue = text.substring(0, ++numc);
        width = tmp.clientWidth;
    }

    // Remove characters until we no longer have overflow
    while(width > max && numc){
        node.nodeValue = text.substring(0, --numc);
        width = tmp.clientWidth;
    }

    // Remove temporary div
    document.body.removeChild(tmp);

    return numc;
}

JSFiddle Example

Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
0

.col { width:40px; overflow: hidden; white-space:nowrap; }

Error: User Rate Limit Exceeded

Error: User Rate Limit ExceededError: User Rate Limit Exceeded

That's odd.
Error: User Rate Limit Exceeded Savas Vedova
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceededjsfiddle.net/trpeters1/qvZKw/16
6

is

<span><span>

function countVisibleCharacters(element) {
    var text = element.firstChild.nodeValue;
    var r = 0;

    element.removeChild(element.firstChild);

    for(var i = 0; i < text.length; i++) {
        var newNode = document.createElement('span');
        newNode.appendChild(document.createTextNode(text.charAt(i)));
        element.appendChild(newNode);

        if(newNode.offsetLeft < element.offsetWidth) {
            r++;
        }
    }

    return r;
}

Error: User Rate Limit Exceeded Savas Vedova
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit ExceededwidthError: User Rate Limit Exceeded#myDiv
2

Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
2

<div class="col">This is my text inside a div and I want the overf|low of the text to be cut</div>

.col { 
   width:120px; 
    overflow: hidden;
   white-space:nowrap;

 }​
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded

Похожие вопросы