Internet explorer dijit.form.TextBox根据上一次选择单击buton时添加字符串
如何在dijit.form.TextBox中失去焦点后仍保持光标位置? 我的表单中有文本框和按钮。当我点击按钮时,我想在我保留的特定光标位置的文本框中添加一些文本 我想要的东西完全像这样: 但这在Internet Explorer中不起作用。Internet explorer dijit.form.TextBox根据上一次选择单击buton时添加字符串,internet-explorer,textbox,dojo,cursor-position,dijit.form,Internet Explorer,Textbox,Dojo,Cursor Position,Dijit.form,如何在dijit.form.TextBox中失去焦点后仍保持光标位置? 我的表单中有文本框和按钮。当我点击按钮时,我想在我保留的特定光标位置的文本框中添加一些文本 我想要的东西完全像这样: 但这在Internet Explorer中不起作用。 有人能帮忙吗?请收听文本框的onblur事件以保存光标位置: textBox.on("blur", function() { var start = this.textbox.selectionStart, end = this.t
有人能帮忙吗?请收听文本框的
onblur
事件以保存光标位置:
textBox.on("blur", function() {
var start = this.textbox.selectionStart,
end = this.textbox.selectionEnd;
this.set("cursorPosition", [start, end]);
});
通过调用textbox.focus()
,以编程方式将焦点返回到textbox时,您可以使用该位置在光标位置插入一些文本,也可以恢复光标位置,例如,当用户单击按钮时:
textBox.on("focus", function() {
var cursorPosition = this.get("cursorPosition");
if(cursorPosition) {
this.textbox.setSelectionRange(cursorPosition[0], cursorPosition[1]);
}
});
请参阅JSFIDLE上的工作示例:收听文本框的
onblur
事件以保存光标位置:
textBox.on("blur", function() {
var start = this.textbox.selectionStart,
end = this.textbox.selectionEnd;
this.set("cursorPosition", [start, end]);
});
通过调用textbox.focus()
,以编程方式将焦点返回到textbox时,您可以使用该位置在光标位置插入一些文本,也可以恢复光标位置,例如,当用户单击按钮时:
textBox.on("focus", function() {
var cursorPosition = this.get("cursorPosition");
if(cursorPosition) {
this.textbox.setSelectionRange(cursorPosition[0], cursorPosition[1]);
}
});
请参阅JSFIDLE上的工作示例:我已经修改了phusick的答案,以满足您的需要 但是,一旦您了解小部件上的cusron选择,这里就真的没有什么可以做的了 HTML:
<input
id="textBox1"
data-dojo-type="dijit.form.TextBox"
value="some text here"/>
<button id="button1" data-dojo-type="dijit.form.Button">foo</button>
require([
"dojo/ready",
"dijit/registry",
"dijit/form/TextBox",
"dijit/form/Button",
"dojo/domReady!"
], function(
ready,
registry
) {
ready(function() {
var textBox = registry.byId("textBox1"),
button = registry.byId("button1");
textBox.on("blur", function() {
var start = this.textbox.selectionStart,
end = this.textbox.selectionEnd;
this.curStart = start;
this.curEnd = end;
});
textBox.on("focus", function() {
var cursorPosition = [this.curStart,this.curEnd];
if(cursorPosition) {
this.textbox.setSelectionRange(cursorPosition[1], cursorPosition[1]);
}
});
button.on("click", function() {
var cur = [textBox.curStart,textBox.curEnd];
var val = textBox.get("value");
var str = val.substring(0,cur[0])+ 'foo' + val.substring(cur[1]);
textBox.set("value", str);
textBox.focus();
textBox.textbox.setSelectionRange(cur[0]+'foo'.length, cur[0]+'foo'.length);
});
});
});
这里是小提琴链接:
我已经修改了phusick的答案,以满足您的需要 但是,一旦您了解小部件上的cusron选择,这里就真的没有什么可以做的了 HTML:
<input
id="textBox1"
data-dojo-type="dijit.form.TextBox"
value="some text here"/>
<button id="button1" data-dojo-type="dijit.form.Button">foo</button>
require([
"dojo/ready",
"dijit/registry",
"dijit/form/TextBox",
"dijit/form/Button",
"dojo/domReady!"
], function(
ready,
registry
) {
ready(function() {
var textBox = registry.byId("textBox1"),
button = registry.byId("button1");
textBox.on("blur", function() {
var start = this.textbox.selectionStart,
end = this.textbox.selectionEnd;
this.curStart = start;
this.curEnd = end;
});
textBox.on("focus", function() {
var cursorPosition = [this.curStart,this.curEnd];
if(cursorPosition) {
this.textbox.setSelectionRange(cursorPosition[1], cursorPosition[1]);
}
});
button.on("click", function() {
var cur = [textBox.curStart,textBox.curEnd];
var val = textBox.get("value");
var str = val.substring(0,cur[0])+ 'foo' + val.substring(cur[1]);
textBox.set("value", str);
textBox.focus();
textBox.textbox.setSelectionRange(cur[0]+'foo'.length, cur[0]+'foo'.length);
});
});
});
这里是小提琴链接:
在该工作示例中,文本即“foo”被添加到末尾,但我希望它被添加到保留的光标位置。假设最初文本框中的文本为“此处的某些文本”,光标位于“文本”中的x处,然后单击按钮后,文本框中的文本应为“此处的某些文本”。按照MozenRath所述的修改操作。谢谢好的,但是你应该重新措辞这个问题。当您需要知道如何使用
子字符串时,不要询问光标位置。不,我想知道如何保留光标位置,因为我不想在末尾添加文本,而是想将其添加到某个指定位置。我没有提到保留“选择”,我的问题是针对“光标位置”。嗨@phusick,我尝试过一些方法,但无法在IE上使用。你能帮忙吗?在该工作示例中,文本即“foo”被添加到末尾,但我希望它被添加到保留的光标位置。假设最初文本框中的文本为“此处的某些文本”,光标位于“文本”中的x处,然后单击按钮后,文本框中的文本应为“此处的某些文本”。按照MozenRath所述的修改操作。谢谢好的,但是你应该重新措辞这个问题。当您需要知道如何使用子字符串时,不要询问光标位置。不,我想知道如何保留光标位置,因为我不想在末尾添加文本,而是想将其添加到某个指定位置。我没有提到保留“选择”,我的问题是针对“光标位置”。嗨@phusick,我已经尝试了一些东西,但我无法让它在IE上工作。你能帮忙吗?