使用htmleditorextender查找文本框的当前光标位置
我一直在使用InsertCaret在文本框的当前光标位置插入文本,但是当文本框与htmleditorextender关联时,它会失败(我猜是因为文本框现在是htmleditorextender的子元素,或者它可能创建了一个iframe?) 这里是我的InsertCaret javascript,它在没有htmleditorextender的情况下运行良好。与htmleditorextender关联的文本框是txtEmailBody。我是否需要以不同方式引用txtEmailBody使用htmleditorextender查找文本框的当前光标位置,htmleditorextender,Htmleditorextender,我一直在使用InsertCaret在文本框的当前光标位置插入文本,但是当文本框与htmleditorextender关联时,它会失败(我猜是因为文本框现在是htmleditorextender的子元素,或者它可能创建了一个iframe?) 这里是我的InsertCaret javascript,它在没有htmleditorextender的情况下运行良好。与htmleditorextender关联的文本框是txtEmailBody。我是否需要以不同方式引用txtEmailBody functio
function insertAtCaret(text) {
var txtarea = document.getElementById("<%=txtEmailBody.ClientID%>")
var scrollPos = txtarea.scrollTop;
var strPos = 0;
var br = ((txtarea.selectionStart || txtarea.selectionStart == '0') ?
"ff" : (document.selection ? "ie" : false));
if (br == "ie") {
txtarea.focus();
var range = document.selection.createRange();
range.moveStart('character', -txtarea.value.length);
strPos = range.text.length;
}
else if (br == "ff") strPos = txtarea.selectionStart;
var front = (txtarea.value).substring(0, strPos);
var back = (txtarea.value).substring(strPos, txtarea.value.length);
txtarea.value = front + text + back;
strPos = strPos + text.length;
if (br == "ie") {
txtarea.focus();
var range = document.selection.createRange();
range.moveStart('character', -txtarea.value.length);
range.moveStart('character', strPos);
range.moveEnd('character', 0);
range.select();
}
else if (br == "ff") {
txtarea.selectionStart = strPos;
txtarea.selectionEnd = strPos;
txtarea.focus();
}
txtarea.scrollTop = scrollPos;
}
函数insertcaret(文本){
var txtarea=document.getElementById(“”)
var scrollPos=txtarea.scrollTop;
var-strPos=0;
var br=((txtarea.selectionStart | | txtarea.selectionStart=='0')?
“ff”:(document.selection?:即:false));
如果(br==“ie”){
txtarea.focus();
var range=document.selection.createRange();
range.moveStart('character',-txtarea.value.length);
strPos=range.text.length;
}
如果(br==“ff”)strPos=txtarea.selectionStart;
var front=(txtarea.value).substring(0,strPos);
var back=(txtarea.value).substring(strPos,txtarea.value.length);
txtarea.value=前面+文本+后面;
strPos=strPos+text.length;
如果(br==“ie”){
txtarea.focus();
var range=document.selection.createRange();
range.moveStart('character',-txtarea.value.length);
range.moveStart('character',strPos);
range.moveEnd('character',0);
range.select();
}
否则如果(br==“ff”){
txtarea.selectionStart=strPos;
txtarea.selectionEnd=strPos;
txtarea.focus();
}
txtarea.scrollpop=scrollPos;
}
谢谢。答案在这里
只需更改对htmleditorextender的引用,如下所示:
$(".btn").click(function () {
var el = $find("<%= HtmlEditorExtender3.ClientID %>")._editableDiv;
pasteHtmlAtCaret("Some <b>random</b> text", el);
});
$(“.btn”)。单击(函数(){
var el=$find(“”);
pasteHtmlAtCaret(“一些随机文本”,el);
});
这将在特定位置插入文本,但我仍然需要找到当前光标位置,以便可以在该位置插入文本:函数insertTextAtCursor(txt){var position=5;var current=$find(“”)。\u editableDiv.innerHTML;//警报(current.substring(0,position))var endVal=current.substring(位置);var newValue=current.substring(0,位置)+txt+endVal;$find(“”)。_editableDiv.innerHTML=newValue;}