Javascript ContentEditable div在JS fiddle中工作不正常
当在JS FIDLE中的Javascript ContentEditable div在JS fiddle中工作不正常,javascript,html,contenteditable,Javascript,Html,Contenteditable,当在JS FIDLE中的contenteditablediv的句子之间写入时,光标的位置会转到句子的末尾 使用向上键代替计时器事件。我已经用了等待树秒,之后钥匙打开。您可以将其更改为增加或减少延迟 <script> var changed, timer, lastValue = '', div = $('#ce'), words = ['oele', 'geel'
contenteditable
div的句子之间写入时,光标的位置会转到句子的末尾
使用向上键代替计时器事件。我已经用了等待树秒,之后钥匙打开。您可以将其更改为增加或减少延迟
<script>
var changed, timer,
lastValue = '',
div = $('#ce'),
words = ['oele', 'geel', 'politie', 'foo bar'];
function markWords() {
var html = div.html().replace(/<\/?strong>/gi, ''),
text = html.replace(/<[^>]+>/g, ' ').replace(/\s+/g, ' '),
exp;
$.each(words, function(i, word) {
exp = new RegExp('\\b(' + word + ')\\b', 'gi');
html = html.replace(exp, function(m) {
// console.log('WORD MATCH:', m);
return '<strong>' + m + '</strong>';
});
});
//html = html.replace(' ', ' ').replace(/\s+/g, ' ');
//console.log('HTML:', html);
//console.log('----');
div.html(html);
}
//setInterval(function() {
// var html = div.html();
// if ( lastValue !== html && html ) {
//console.log(lastValue);
//console.log(html);
////console.log('----');
// lastValue = html;
// markWords();
// setEndOfContenteditable(div[0]);
// }
//}, 500);
$("#ce").on('keyup', function() {
clearTimeout(timer); //clear any running timeout on key up
timer = setTimeout(function() { //then give it a second to see if the user is finished
//do
var html = div.html();
if ( lastValue !== html && html ) {
// console.log(lastValue);
// console.log(html);
//console.log('----');
lastValue = html;
markWords();
setEndOfContenteditable(div[0]);
}
}, 3000);
});
function setEndOfContenteditable(contentEditableElement)
{
var range,selection;
if(document.createRange)//Firefox, Chrome, Opera, Safari, IE 9+
{
range = document.createRange();//Create a range (a range is a like the selection but invisible)
range.selectNodeContents(contentEditableElement);//Select the entire contents of the element with the range
range.collapse(false);//collapse the range to the end point. false means collapse to end rather than the start
selection = window.getSelection();//get the selection object (allows you to change selection)
selection.removeAllRanges();//remove any selections already made
selection.addRange(range);//make the range you have just created the visible selection
}
else if(document.selection)//IE 8 and lower
{
range = document.body.createTextRange();//Create a range (a range is a like the selection but invisible)
range.moveToElementText(contentEditableElement);//Select the entire contents of the element with the range
range.collapse(false);//collapse the range to the end point. false means collapse to end rather than the start
range.select();//Select the range (make it the visible selection
}
}
markWords();
setEndOfContenteditable(div[0]);
</script>
变量改变了,计时器,
lastValue=“”,
div=$('ce'),
单词=['oele','geel','politie','foobar'];
函数标记词(){
var html=div.html().replace(//gi',),
text=html.replace(/]+>/g')。replace(/\s+//g'),
经验;
$.each(单词、函数(i、单词){
exp=newregexp('\\b('+word+')\\b','gi');
html=html.replace(exp,function(m){
//log('wordmatch:',m);
返回“”+m+””;
});
});
//html=html.replace(“”,).replace(/\s+/g,);
//log('HTML:',HTML);
//console.log('---');
div.html(html);
}
//setInterval(函数(){
//var html=div.html();
//if(lastValue!==html&&html){
//console.log(lastValue);
//log(html);
////console.log('---');
//lastValue=html;
//标记词();
//setEndOfContenteditable(div[0]);
// }
//}, 500);
$(“#ce”).on('keyup',function(){
clearTimeout(timer);//在键向上时清除任何运行超时
timer=setTimeout(function(){//然后给它一秒钟时间看看用户是否完成了
//做
var html=div.html();
if(lastValue!==html&&html){
//console.log(lastValue);
//log(html);
//console.log('---');
lastValue=html;
标记词();
setEndOfContenteditable(div[0]);
}
}, 3000);
});
函数setEndOfContenteditable(contentEditableElement)
{
var范围、选择;
if(document.createRange)//Firefox、Chrome、Opera、Safari、IE 9+
{
range=document.createRange();//创建一个范围(一个范围与所选内容类似,但不可见)
range.selectNodeContents(contentEditableElement);//选择具有范围的元素的全部内容
range.collapse(false);//将范围折叠到终点。false表示折叠到终点而不是起点
selection=window.getSelection();//获取选择对象(允许您更改选择)
selection.removeAllRanges();//删除所有已做的选择
selection.addRange(range);//使刚刚创建的范围成为可见的选择
}
else if(document.selection)//IE 8及更低版本
{
range=document.body.createTextRange();//创建一个范围(范围与所选内容类似,但不可见)
range.moveToElementText(contentEditableElement);//使用范围选择元素的全部内容
range.collapse(false);//将范围折叠到终点。false表示折叠到终点而不是起点
range.select();//选择范围(使其成为可见选择
}
}
标记词();
setEndOfContenteditable(div[0]);
使用向上键代替计时器事件。我使用了向上键后的等待树秒。您可以将其更改为增加或减少延迟
<script>
var changed, timer,
lastValue = '',
div = $('#ce'),
words = ['oele', 'geel', 'politie', 'foo bar'];
function markWords() {
var html = div.html().replace(/<\/?strong>/gi, ''),
text = html.replace(/<[^>]+>/g, ' ').replace(/\s+/g, ' '),
exp;
$.each(words, function(i, word) {
exp = new RegExp('\\b(' + word + ')\\b', 'gi');
html = html.replace(exp, function(m) {
// console.log('WORD MATCH:', m);
return '<strong>' + m + '</strong>';
});
});
//html = html.replace(' ', ' ').replace(/\s+/g, ' ');
//console.log('HTML:', html);
//console.log('----');
div.html(html);
}
//setInterval(function() {
// var html = div.html();
// if ( lastValue !== html && html ) {
//console.log(lastValue);
//console.log(html);
////console.log('----');
// lastValue = html;
// markWords();
// setEndOfContenteditable(div[0]);
// }
//}, 500);
$("#ce").on('keyup', function() {
clearTimeout(timer); //clear any running timeout on key up
timer = setTimeout(function() { //then give it a second to see if the user is finished
//do
var html = div.html();
if ( lastValue !== html && html ) {
// console.log(lastValue);
// console.log(html);
//console.log('----');
lastValue = html;
markWords();
setEndOfContenteditable(div[0]);
}
}, 3000);
});
function setEndOfContenteditable(contentEditableElement)
{
var range,selection;
if(document.createRange)//Firefox, Chrome, Opera, Safari, IE 9+
{
range = document.createRange();//Create a range (a range is a like the selection but invisible)
range.selectNodeContents(contentEditableElement);//Select the entire contents of the element with the range
range.collapse(false);//collapse the range to the end point. false means collapse to end rather than the start
selection = window.getSelection();//get the selection object (allows you to change selection)
selection.removeAllRanges();//remove any selections already made
selection.addRange(range);//make the range you have just created the visible selection
}
else if(document.selection)//IE 8 and lower
{
range = document.body.createTextRange();//Create a range (a range is a like the selection but invisible)
range.moveToElementText(contentEditableElement);//Select the entire contents of the element with the range
range.collapse(false);//collapse the range to the end point. false means collapse to end rather than the start
range.select();//Select the range (make it the visible selection
}
}
markWords();
setEndOfContenteditable(div[0]);
</script>
变量改变了,计时器,
lastValue=“”,
div=$('ce'),
单词=['oele','geel','politie','foobar'];
函数标记词(){
var html=div.html().replace(//gi',),
text=html.replace(/]+>/g')。replace(/\s+//g'),
经验;
$.each(单词、函数(i、单词){
exp=newregexp('\\b('+word+')\\b','gi');
html=html.replace(exp,function(m){
//log('wordmatch:',m);
返回“”+m+””;
});
});
//html=html.replace(“”,).replace(/\s+/g,);
//log('HTML:',HTML);
//console.log('---');
div.html(html);
}
//setInterval(函数(){
//var html=div.html();
//if(lastValue!==html&&html){
//console.log(lastValue);
//log(html);
////console.log('---');
//lastValue=html;
//标记词();
//setEndOfContenteditable(div[0]);
// }
//}, 500);
$(“#ce”).on('keyup',function(){
clearTimeout(timer);//在键向上时清除任何运行超时
timer=setTimeout(function(){//然后给它一秒钟时间看看用户是否完成了
//做
var html=div.html();
if(lastValue!==html&&html){
//console.log(lastValue);
//log(html);
//console.log('---');
lastValue=html;
标记词();
setEndOfContenteditable(div[0]);
}
}, 3000);
});
函数setEndOfContenteditable(contentEditableElement)
{
var范围、选择;
if(document.createRange)//Firefox、Chrome、Opera、Safari、IE 9+
{
range=document.createRange();//创建一个范围(一个范围与所选内容类似,但不可见)
range.selectNodeContents(contentEditableElement);//选择整个目录