日期:2014-05-18  浏览次数:20465 次

处理文本框中选中的文字,IE6下通过帮解决火狐和其它浏览器
JScript code


function edit(TextId, Text1, Text2) {
    if ((document.selection) && (document.selection.type == "Text") && (document.selection.createRange().parentElement().id == TextId)) {

        document.selection.createRange().text = Text1 + document.selection.createRange().text + Text2;
        document.selection.empty();
    }
    else {
        alert("先在输入框中选中你要改变的文字。");
    }
}


上面代码在本机默认IE6通过,功能发挥正常,,火狐里不行,都执行的ELSE里的提示,不知道是哪个方法不适用,或者用别的方法能实现上面的功能又能很兼容的?

我要选中指定文本框里的内容,然后点按扭,改变选中的内容(前后加括号什么的),,同页面下可能有两到三个文本框

,,

感谢帮忙解决的朋友,祝你们生活愉快,,,

------解决方案--------------------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<input id="x" style="width:600px">
<input type="button" value="测试" onclick="edit('x','插入的左边内容','插入的右边内容')" />
<script>
function edit(TextId, Text1, Text2) {
if ((document.selection)) {
document.selection.createRange().text = Text1 + document.selection.createRange().text + Text2;
document.selection.empty();
}
else {
start = document.getElementById(TextId).selectionStart;
end = document.getElementById(TextId).selectionEnd;
t = document.getElementById(TextId).value;
t1 = t.substr(0, start);
t2 = t.substr(start, end - start);
t3 = t.substr(end);
document.getElementById(TextId).value = t1 + Text1 + t2 + Text2 + t3;
}
}
</script>