简体   繁体   中英

Add text after span text not within the span tag

Check this screenshot:

在此处输入图片说明

I am using this code to add span of emoji in DIV.

$('body').on('click', '.selectable-icons', function(e) {
    if (e.target.tagName.toLowerCase() === 'span') {
        document.querySelector('[contenteditable]').appendChild(e.target.cloneNode(true));
        $('[contenteditable]').append(" ");
        var current_text = $('[contenteditable]').html();
        alert(current_text);
        $('.publisher_div').html(current_text);
    }

But space is not appending after span tag, so that I can write next text after span tag.

$('[contenteditable]').append(" "); will append a space to the elements with a contenteditable attribute (the span , in your case). If you want to add the space after , use http://api.jquery.com/after/ :

$('[contenteditable]').after(" ");

Live Example:

 var container = $("#container"); console.log("Before:"); container.contents().each(function(index) { console.log(index, this.nodeType, this.nodeType === 3 ? '"' + this.nodeValue + '"' : this.tagName); }); $('[contenteditable]').after(" "); console.log("After:"); container.contents().each(function(index) { console.log(index, this.nodeType, this.nodeType === 3 ? '"' + this.nodeValue + '"' : this.tagName); }); 
 <div id="container"><span contenteditable>This is the span</span></div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

尝试这个

  $('.publisher_div').append("hi");

尝试:

$('[contenteditable]').append("&nbsp;");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM