首页 > 解决方案 > 字符有限

问题描述

在 HTML < p > 上限制字符的最短方法是什么?我想让元素输入,并删除边框、背景等,看起来就像纯文本和 maxlength = 5; 但我想知道是否有其他解决方案。谢谢你!

    let recipeNameShow = document.createElement('p');
    recipeNameShow.classList.add('fntColor');
    recipeNameShow.id = 'recipeNameShow';
    recipeNameShow.innerText = newRecipeName;

标签: javascripthtmlcss

解决方案


如果您不期望用户输入,您可以使用substr

let recipeNameShow = document.createElement('p');
recipeNameShow.classList.add('fntColor');
recipeNameShow.id = 'recipeNameShow';
recipeNameShow.innerText = newRecipeName.substr(0,5);

推荐阅读