如何巧妙地在javascript中截断HTML字符串?

时间:2014-04-02 19:52:53

标签: javascript jquery string

我有一个用户输入的内容,可以是任何类型的文本。

例如

  

这是一些示例文本。我可以使用特殊字符,例如<>&<>&<>&<>&<>&<>&<>&< >&安培;<>&安培;<>&安培;

然后我在网格中显示此内容,其中文本被截断为80个字符(以适应可用空间),如果用户将文本鼠标悬停在文本上,则可以看到完整的字符串。

我有一个功能,我打电话给我这样做。

function GetDescriptionContent(data, type, row){            
    return 
        "<span title='" + data.replace(/'/g, '&#39;') + "'>" + 
            $('<div/>').text(data).html().substring(0, 80) + 
        "</span>";
}

然而,当我对html进行细分时,我有时会删除像&amp; &lt; &gt;这样的特殊字符。 此外,当字符串中有一个特殊字符时,它会过早地剪掉字符串,否则可能会很长。

如何在javascript中巧妙地截断我的HTML字符串?

1 个答案:

答案 0 :(得分:4)

我发现我可以跟踪html实体并调整截断长度。

function truncateToLength(
    stringToTruncate, 
    truncationLength, 
    truncationCharacter = "&hellip;")
{
    //string is too small, does not need to be truncated
    if(stringToTruncate.length <= truncationLength){
        return stringToTruncate;
    }

    //find all html entities
    var splitOnAmpersandArray = stringToTruncate.split('&');

    //first instance of html entity is beyond our truncation length
    //return what we have plus truncation character
    if(splitOnAmpersandArray[0].length > truncationLength){
        return splitOnAmpersandArray[0].substring(0, truncationLength) 
            + truncationCharacter;
    }

    //first instance of html entity is inside our truncation length
    var truncatedString = splitOnAmpersandArray[0];

    //keep adding onto truncated string until:
    // it is longer than our length or 
    // we are out of characters to add on.
    for(var i = 1; i < splitOnAmpersandArray.length; i++){
        //find end of current html entity
        var htmlEntityLength = splitOnAmpersandArray[i].indexOf(';');

        //increase truncation length to account for size of current html entity
        truncationLength += htmlEntityLength + 1;

        //add up until next html entity
        truncatedString = truncatedString + '&' + splitOnAmpersandArray[i];

        //if our new length is too long, truncate and add truncation character
        if(truncatedString.length >= truncationLength){
            return truncatedString.substring(0,truncationLength) 
                + truncationCharacter;
        }
    }

    //we ran out of characters to add onto string, return result
    return truncatedString;
}

  var content = "&nbsp;&amp;&gt;&lt;&quot;&apos;&cent;&pound;&yen;&euro;&copy;&reg;";
  $('#sample').html(truncateToLength(content, 5));
  $('#sample').prop('title', $('<div/>').html(content).text());




    function truncateToLength(
        stringToTruncate, 
        truncationLength, 
        truncationCharacter = "&hellip;")
    {
        //string is too small, does not need to be truncated
        if(stringToTruncate.length <= truncationLength){
            return stringToTruncate;
        }
        
        //find all html entities
        var splitOnAmpersandArray = stringToTruncate.split('&');

        //first instance of html entity is beyond our truncation length
        //return what we have plus truncation character
        if(splitOnAmpersandArray[0].length > truncationLength){
            return splitOnAmpersandArray[0].substring(0, truncationLength) 
                + truncationCharacter;
        }

        //first instance of html entity is inside our truncation length
        var truncatedString = splitOnAmpersandArray[0];
        
        //keep adding onto truncated string until:
        // it is longer than our length or 
        // we are out of characters to add on.
        for(var i = 1; i < splitOnAmpersandArray.length; i++){
            //find end of current html entity
            var htmlEntityLength = splitOnAmpersandArray[i].indexOf(';');

            //increase truncation length to account for size of current html entity
            truncationLength += htmlEntityLength + 1;

            //add up until next html entity
            truncatedString = truncatedString + '&' + splitOnAmpersandArray[i];

            //if our new length is too long, truncate and add truncation character
            if(truncatedString.length >= truncationLength){
                return truncatedString.substring(0,truncationLength) 
                    + truncationCharacter;
            }
        }

        //we ran out of characters to add onto string, return result
        return truncatedString;
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="sample"></div>