window.getSelection(),document.selection浏览器兼容问题
<html>
<head>
<title> New Document </title>
<SCRIPT>
function getCaretForTextArea(ZysrID){
var txb = document.getElementById(ZysrID);
var pos = 0;
txb.focus();
var s = txb.scrollTop;
var r = document.selection.createRange();
var t = txb.createTextRange();
t.collapse(true);
t.select();
var j = document.selection.createRange();
r.setEndPoint("StartToStart",j);
var str = r.text;
var re = new RegExp("[\\n]","g");
str = str.replace(re,"");
pos = str.length;
alert(pos);
r.collapse(false);
r.select();
txb.scrollTop = s;
}
</SCRIPT>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
</head>
<body>
<textarea id="eimarea" col="5" row="5" onkeyup="getCaretForTextArea('eimarea')"></textarea><br/>
</body>
</html>
如题,我采用了这种方法解决selection的兼容性:
if (window.getSelection) {
r=window.getSelection();
} else if (document.getSelection) {
r=document.getSelection();
} else if (document.selection) {
r=document.selection.createRange();
}
但是我遇到的情况是接下来的方法var t =txb.createTextRange();t.collapse(true);t.select();r.setEndPoint("StartToStart",j);都是依据IE写的,在Firefox和Chrome里下面的方法都有问题,求解相应浏览器这些方法的替代方法。
------解决方案--------------------
我也遇到这么个问题帮顶~