猿问

内容可编辑,在文本末尾设置插入符号(跨浏览器)

内容可编辑,在文本末尾设置插入符号(跨浏览器)

输出:

<div id="content" contenteditable="true" style="border:1px solid #000;width:500px;height:40px;">
    hey    <div>what's up?</div><div><button id="insert_caret"></button>

我相信法夫它看起来会是这样的:

hey<br />what's up?

和在:

hey<p>what's up?</p>

不幸的是,没有很好的方法使每个浏览器都插入一个<br />而不是一个div或者p标签,或者至少我在网上找不到任何东西。


不管怎样,我现在想做的是,当我击中扣子,我希望将插入符号设置在文本的末尾,因此应该如下所示:

hey
what's up?|

任何方法都可以做到这一点全浏览器?

例子:

$(document).ready(function(){
    $('#insert_caret').click(function()
    {
        var ele = $('#content');
        var length = ele.html().length;

        ele.focus();

        //set caret -> end pos
     }
 }


慕妹3146593
浏览 520回答 3
3回答
随时随地看视频慕课网APP
我要回答