1 回答

TA貢獻1863條經驗 獲得超2個贊
這是一種方法,可以輕松修改它以處理特定的文本輸入,但這可以確保頁面上的每個文本輸入從剪貼板獲取相同的數據。
旁注:querySelectorAll返回 anodelist而不是數組,您可以在 a 中[].forEach.call使用數組的方法。forEachnodelist
// Listen to paste on the document
document.addEventListener("paste", function(e) {
// if the target is a text input
if (e.target.type === "text") {
var data = e.clipboardData.getData('Text');
// split clipboard text into single characters
data = data.split('');
// find all other text inputs
[].forEach.call(document.querySelectorAll("input[type=text]"), (node, index) => {
// And set input value to the relative character
node.value = data[index];
});
}
});
<input type="text" maxlength="1">
<input type="text" maxlength="1">
<input type="text" maxlength="1">
<input type="text" maxlength="1">
<input type="text" maxlength="1">
<input type="text" maxlength="1">
- 1 回答
- 0 關注
- 112 瀏覽
添加回答
舉報