本文介绍了如何在TextBox或TextArea中获取位置光标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
hi
<HTML>
<HEAD><TITLE>Selection</TITLE>
<SCRIPT type="text/javascript">
function disp()
{
var text = document.getElementById("text");
//var t = text.value.substr(text.selectionStart,text.selectionEnd-text.selectionStart);
//alert(t+"\nSelectionStart:"+text.selectionStart);
}
</SCRIPT>
</HEAD>
<BODY>
<TEXTAREA id="text">Hello, How are You?</TEXTAREA><BR>
<INPUT type="button" value="Select text and click here" önclick="disp();"/>
</BODY>
</HTML>
SelectionStart不适用于IE但在FireFox工作。
请帮帮我
SelectionStart is not Work In IE but In FireFox Work.
Please Help me
推荐答案
var pos = 0;
if("selectionStart" in el) {
pos = el.selectionStart;
} else if("selection" in document) {
el.focus();
var Sel = document.selection.createRange();
var SelLength = document.selection.createRange().text.length;
Sel.moveStart("character", -el.value.length);
pos = Sel.text.length - SelLength;
}
return pos;
let cursorIndex =
这篇关于如何在TextBox或TextArea中获取位置光标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!