📜  限制 html 元素内动态文本的长度 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:04:16.294000             🧑  作者: Mango

代码示例1
function truncateText(selector, maxLength) {
    var element = document.querySelector(selector),
        truncated = element.innerText;

    if (truncated.length > maxLength) {
        truncated = truncated.substr(0,maxLength) + '...';
    }
    return truncated;
}
//You can then call the function with something like what i have below.
document.querySelector('p').innerText = truncateText('p', 107);