2 回答
TA贡献1829条经验 获得超9个赞
将您的输入设置为 p 元素并更改此行
counterRef.value = "Characters remaining: " + remLength + "/200";
到
counterRef.innerHTML = "Characters remaining: " + remLength + "/200";
TA贡献1936条经验 获得超6个赞
input 标签具有 value 属性,而 p 标签应使用 textContent 属性:
var txtBoxRef = document.querySelector("#txtBox");
var counterRef = document.querySelector("#counterBox");
var pRef = document.querySelector("#pBox");
txtBoxRef.addEventListener("keydown",function(){
var remLength = 0;
remLength = 0 + parseInt(txtBoxRef.value.length);
if(remLength < 0) {
txtBoxRef.value = txtBoxRef.value.substring(0, 200);
return false;
} else if(remLength > 200) {
counterRef.style.color = "red";
}
pRef.textContent = "Characters remaining: " + remLength + "/200";
},true);
<textarea style="width: 600px;" id="txtBox"></textarea>
<input type="text" id="counterBox"/>
<p id='pBox'></p>
- 2 回答
- 0 关注
- 115 浏览
添加回答
举报