//获取input/textarea中选择的文本
function getSelectedText(textbox){
if (document.selection){//IE
return document.selection.createRange().text;
}
else {
return textbox.value.substring(textbox.selectionStart,
textbox.selectionEnd);
}
}
//设置input/textarea中选中的文本
function selectText(textbox, startIndex, stopIndex){
if (textbox.setSelectionRange){
textbox.setSelectionRange(startIndex, stopIndex);
}
else if (textbox.createTextRange){//IE
var range = textbox.createTextRange();
range.collapse(true);
range.moveStart('character', startIndex);
range.moveEnd('character', stopIndex - startIndex);
range.select();
}
textbox.focus();
}
input/textarea文本的选择与获取
最新推荐文章于 2025-05-23 20:30:16 发布
本文介绍了一种使用JavaScript来获取及设置HTML输入框或文本域中选定文本的方法。这些方法适用于跨浏览器环境,包括IE和其他现代浏览器。通过这些实用的函数,开发者可以轻松地实现文本编辑功能。
1244

被折叠的 条评论
为什么被折叠?



