取得光标位置,上网搜搜,多的是!
取得selection对象的内容,很简单,给个例子吧:
<script>
var tid = null;
function abc(){
var txt1=document.getElementById("txt1");
alert( document.selection.createRange().text);
}
}</script>
<body>
<div id="div_id" style="width:300px;height:300px;border:1px solid #FF0000;"></div>
<input type="button" value="取得选中的文字" onclick='abc();'>asdffhmgfd
<input type="text" value="euifoswfjudgspf" id="txt1">
</body>
取得selection对象的内容,很简单,给个例子吧:
<script>
var tid = null;
function abc(){
var txt1=document.getElementById("txt1");
alert( document.selection.createRange().text);
}
}</script>
<body>
<div id="div_id" style="width:300px;height:300px;border:1px solid #FF0000;"></div>
<input type="button" value="取得选中的文字" onclick='abc();'>asdffhmgfd
<input type="text" value="euifoswfjudgspf" id="txt1">
</body>
<script type="text/javascript">
var get_selection=function() {
if (window.getSelection) {
alert(window.getSelection());
} else if (document.getSelection) {
alert(document.getSelection());
} else if (document.selection) {
alert(document.selection.createRange().text);
}
}
</script>
</head><body>
This is an example of geting selection<textarea>This is an example of geting selection</textarea><input type="button" value="get selection" onmousedown="get_selection();"></body></html>
<script>
var sb = new Array(
'leftbox', 'rightbox', 'scrollLeft', 'scrollRight'
)for (var i in sb) eval('var ' + sb[i] + ' = {}')var os = 0
var oe = 0function update(o) {
var t = o.value, s = getSelectionStart(o), e = getSelectionEnd(o)
if (s == os && e == oe) return
rightbox.value = scrollRight.firstChild.nodeValue = t.substring(s).replace(/ /g, '\xa0') || '\xa0'
leftbox.value = scrollLeft.firstChild.nodeValue = t.substring(0, s).replace(/ /g, '\xa0') || '\xa0' os = s
oe = e
return true
}function setup() {
for (var i in sb) eval(sb[i] + ' = document.getElementById(sb[i])')
}function getSelectionStart(o) {
if (o.createTextRange) {
var r = document.selection.createRange().duplicate()
r.moveEnd('character', o.value.length)
if (r.text == '') return o.value.length
return o.value.lastIndexOf(r.text)
} else return o.selectionStart
}function getSelectionEnd(o) { if (o.createTextRange) {
var r = document.selection.createRange().duplicate()
r.moveStart('character', -o.value.length)
return r.text.length
} else return o.selectionEnd}
</script>
<body onload="setup()">
<div>
<p><input class="input" value="This is an example"
onclick="return(update(this))"
onkeyup="return(update(this))"
onkeydown="return(update(this))"
onkeypress="return(update(this))"
onmouseup="return(update(this))"
onmousedown="return(update(this))" /></p>
</div>
<p>
<input id="leftbox" maxlength="32" value="TEST" disabled /><input id="rightbox" maxlength="32" value="TEST" disabled />
</p>
<p>
<span id="scrollLeft">left</span>|
<span id="scrollRight">right</span>
</p>
</body>
</html>