87,916
社区成员
发帖
与我相关
我的任务
分享
<script type="text/javascript">
function getText(v) {
var ta = document.getElementById('ta');
if (document.selection)//IE
{
alert(document.selection.createRange().text);
if (v) document.selection.createRange().text = v;
}
else if (ta.selectionStart) {
alert(ta.value.substring(ta.selectionStart, ta.selectionEnd));
if (v) ta.value = ta.value.substring(0, ta.selectionStart) + v + ta.value.substring(ta.selectionEnd);
}
}
</script>
<input type="button" value="获取内容" onclick="getText()"/>
<input type="button" value="替换选择内容为123" onclick="getText('123')"/>
<textarea id="ta" rows="5" cols="30"></textarea>