小编Kel*_*Lim的帖子

如何在键入时格式化contenteditable div?

我正在尝试编写一个函数,允许一个contenteditable div在用户输入div时进行一些自动格式化.到目前为止,我只能让它在IE中运行.有人可以帮帮我吗?

function formatOnKeyUp(){
    if (window.getSelection) {
        // ???????
    } else if (document.selection) {
        cursorPos=document.selection.createRange().duplicate();
        clickx = cursorPos.getBoundingClientRect().left; 
        clicky = cursorPos.getBoundingClientRect().top;
    }

    text = document.getElementById('div1').innerHTML;
    text = text.replace(/this/gm, "<i>this</i>");
    // .... some other formating here...
    document.getElementById('div1').innerHTML = text;

    if (window.getSelection) {
        // ????????
    } else if (document.selection) {
        cursorPos = document.body.createTextRange();
        cursorPos.moveToPoint(clickx, clicky);
        cursorPos.select();
    }
}
Run Code Online (Sandbox Code Playgroud)

html javascript html5 contenteditable

5
推荐指数
1
解决办法
3401
查看次数

标签 统计

contenteditable ×1

html ×1

html5 ×1

javascript ×1