js 表單操作補充(取得表單欄位元素,input選擇文字、獲取選擇文字的值)
阿新 • • 發佈:2018-12-16
var form = document.getElementById("myForm"); var field = form.elements[0]; // 選擇文字 field.select() // 選擇部分文字 field.setSelectionRange(0, 3); // 獲取選中文字的值 function getSelectedText(textbox) { if (typeof textbox.selectionStart == "number") { return textbox.value.substring(textbox.selectionStart,textbox.selectionEnd); } else if (document.selection) { return document.selection.createRange().text; } }