[英]how to remove the character at which cursor is placed in an input box on button click
function remove() { }
<input id="input-box"></input> <button onclick="remove()" id="remove-btn">remove</button>
我想刪除 cursor 在按鈕單擊時放置在輸入框中的特定字母。
使用selectionStart
屬性
function remove() { const input = document.querySelector('#input-box'); if (input.value.length > 0) { const pos = input.selectionStart; if (pos > 0) { input.value = input.value.substr(0, pos - 1) + input.value.substr(pos); input.focus(); input.selectionStart = pos - 1; input.selectionEnd = pos - 1; } } }
<input id="input-box"></input> <button onclick="remove()" id="remove-btn">remove</button>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.