这是代码(现在已满):
HTML:
<div id="content" contentEditable="true" onkeyup="highlight(this)">This is some area to type.</div>
Javascript:
function highlight(elem){
// store cursor position
var cursorPos=document.selection.createRange().duplicate();
var clickx = cursorPos.getBoundingClientRect().left;
var clicky = cursorPos.getBoundingClientRect().top;
// copy contents of div
var content = elem.innerHTML;
var replaceStart = '';
var replaceEnd = '';
// only replace/move cursor if any matches
// note the spacebands - this prevents duplicates
if(content.match(/ test /)) {
elem.innerHTML = content.replace(/ test /g,' '+replaceStart+'test'+replaceEnd+' ');
// reset cursor and focus
cursorPos = document.body.createTextRange();
cursorPos.moveToPoint(clickx, clicky);
cursorPos.select();
}
}
只是在IE上工作,不高兴。任何人都可以“调整”此代码,也可以在 FF 上工作!...
谢谢
编辑[1]:
Div 可编辑和更多... 更多
此代码用格式化的相同单词替换特定单词......
并且插入符号(光标)始终在替换单词之后保留!<<<“这是大事”
但只适用于IE,我非常喜欢重写这段代码以在FF上工作......但我做不到......太难了......
任何人都可以帮忙?
编辑[2]:我的问题只是这部分:
// reset cursor and focus
cursorPos = document.body.createTextRange();
cursorPos.moveToPoint(clickx, clicky);
cursorPos.select();
因为,moveToPotion 和 select 函数只能在 IE 上运行......直到那时它很容易......在 FF 上还有另一组函数可以实现......但我不知道如何编写另一个代码来执行一样的东西。你明白了吗?