gpt4 book ai didi

javascript - 根据输入的字符数更改字体

转载 作者:行者123 更新时间:2023-11-28 16:06:39 25 4
gpt4 key购买 nike

如何根据输入的字符数使用 JavaScript 在输入字段上更改字体大小。例如,默认字体大小是16px,但是如果我添加超过10个字符,字体大小就会变成12px。

HTML:

<div class="phone-input">
<input readonly="readonly" type="text" id="tocall" value="">

和CSS:

input#tocall {
width: 145px;
padding: 6px 3px;
color: #424242;
font-size: 16px;
border: 1px solid rgb(224, 224, 224);
font-weight: bold;
letter-spacing: 0.1em;
}

我不熟悉 JavaScript,所以请帮助我。

最佳答案

您可以执行类似于下面代码片段的操作,如果您将 readonly 保留在 input 之外,现在您将无法在其中输入任何内容。

var input = document.getElementById('tocall');
input.addEventListener('keyup', function (e) {
if (e.target.value.length > 10) {
e.target.style.fontSize = '12px';
} else {
e.target.style.fontSize = '16px';
}
return;
}, false);

关于javascript - 根据输入的字符数更改字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14664090/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com