Javascript 如何创建文本一个接一个地返回以修复拼写
我已经在占位符中插入了一个文本。它工作正常。现在,在显示文本后,我希望光标转到特定点(此处为“怀疑”),并更正拼写错误的单词(怀疑到怀疑) 怎么做?你能给我举个我想在这个项目中做的例子吗 代码###Javascript 如何创建文本一个接一个地返回以修复拼写,javascript,css,animation,textbox,slideshow,Javascript,Css,Animation,Textbox,Slideshow,我已经在占位符中插入了一个文本。它工作正常。现在,在显示文本后,我希望光标转到特定点(此处为“怀疑”),并更正拼写错误的单词(怀疑到怀疑) 怎么做?你能给我举个我想在这个项目中做的例子吗 代码### var txt=“请写下您的信息。如果有任何疑问,请随时提问!”; var超时; var txtLen=txt.length; var-char=0; $('textarea').attr('placeholder','|'); (函数类型it(){ var humanize=Math.round
var txt=“请写下您的信息。如果有任何疑问,请随时提问!”;
var超时;
var txtLen=txt.length;
var-char=0;
$('textarea').attr('placeholder','|');
(函数类型it(){
var humanize=Math.round(Math.random()*(200-30))+30;
timeOut=setTimeout(函数(){
char++;
var type=txt.substring(0,char);
$('textarea').attr('placeholder',type+'|');
键入它();
if(char==txtLen){
$('textarea').attr('placeholder',$('textarea').attr('placeholder').slice(0,-1))//删除“|”
clearTimeout(超时);
}
}人性化);
}());代码>
首先,添加另一个变量来保存修改后的、无打字错误的字符串,这样我们仍然可以遍历原始字符串。此新变量将用于显示文本
var modified_txt = "";
如果你知道排版在字符串中的位置,你可以制作一个对象来检查排版的位置
//Positions in the txt string where typos exist
var typos = {
38: {},
25: {}
}
随着字符计数器的增加,您可以对照对象检查它
var test = typos[char];
if (test !== undefined) {
//Typo found do something with it
}
在本例中,我选择编写两个新函数,一个用于添加字符,一个用于删除字符
function deleteCharacter(text) {
text = text.substring(0, text.length - 1);
return text;
}
function addCharacter(text, character_added) {
text = text + character_added;
return text;
}
我还决定让其中一个typo对象属性成为一个函数,这样我们就可以在typo对象中组织我们的typo并执行我们想要的操作
var typos = {
38: {
error: 's',
correction: function(text) {
var temp = deleteCharacter(text);
$('textarea').attr('placeholder', temp + '|');
return temp;
}
}
}
现在我们可以在发现输入错误时进行函数调用
if (test !== undefined) {
//Typo found do something with it
setTimeout(function() {
var chunk_one = test.correction(modified_txt);
modified_txt = chunk_one;
char++;
typeIt();
}, humanize());
} else { //If no typos are found then move to the next character
setTimeout(function() {
char++;
typeIt();
}, humanize());
}
首先,添加另一个变量来保存修改后的、无打字错误的字符串,这样我们仍然可以遍历原始字符串。此新变量将用于显示文本
var modified_txt = "";
如果你知道排版在字符串中的位置,你可以制作一个对象来检查排版的位置
//Positions in the txt string where typos exist
var typos = {
38: {},
25: {}
}
随着字符计数器的增加,您可以对照对象检查它
var test = typos[char];
if (test !== undefined) {
//Typo found do something with it
}
在本例中,我选择编写两个新函数,一个用于添加字符,一个用于删除字符
function deleteCharacter(text) {
text = text.substring(0, text.length - 1);
return text;
}
function addCharacter(text, character_added) {
text = text + character_added;
return text;
}
我还决定让其中一个typo对象属性成为一个函数,这样我们就可以在typo对象中组织我们的typo并执行我们想要的操作
var typos = {
38: {
error: 's',
correction: function(text) {
var temp = deleteCharacter(text);
$('textarea').attr('placeholder', temp + '|');
return temp;
}
}
}
现在我们可以在发现输入错误时进行函数调用
if (test !== undefined) {
//Typo found do something with it
setTimeout(function() {
var chunk_one = test.correction(modified_txt);
modified_txt = chunk_one;
char++;
typeIt();
}, humanize());
} else { //If no typos are found then move to the next character
setTimeout(function() {
char++;
typeIt();
}, humanize());
}
这里是codepen中的链接这里是codepen中的链接谢谢@Brad,这对我帮助很大。请再说一件事,我希望错误会先保留并完成句子。然后“|”会出现在“s”之前,然后删除并淡出。如果你想在最后纠正拼写错误,同样的想法也会适用。在字符串完全打印出来后进行拼写检查,然后返回并将“|”添加到正确的位置,并根据需要删除或添加字符。谢谢@Brad,这对我帮助很大。请再说一件事,我希望错误将首先保留并完成句子。然后“|”将出现在“s”之前,然后删除并淡出。如果你想在最后纠正打字错误,同样的想法也适用。在字符串完全打印出来后进行打字检查,然后返回并将“|”添加到正确的位置,并根据需要删除或添加字符。