开发者

Adding DOM elements using contentEditable

开发者 https://www.devze.com 2022-12-31 05:46 出处:网络
I\'ve created an editable div, and I want to replace smiley signs with smiley images. But whenever I replace a string with a dom element (<img> or <span> or whatever..), the div stops bei

I've created an editable div, and I want to replace smiley signs with smiley images.

But whenever I replace a string with a dom element (<img> or <span> or whatever..), the div stops being editable (i.e. I can see the caret when I click on text, but can add no characters to it).

What's going on? (I'm doing this in Safari)

Here's my code:

var txtInput = document.getElementById("asdf");
txtInput.contentEditable = true;
txtInput.addEventListener("textInput", fu开发者_如何学运维nction(event){
    var str = txtInput.innerHTML;
    txtInput.innerHTML = str.replace("f", "<span>w<span>"); 
}, false);


I think you should be using execCommand with inserthtml or insertimage, though I can't get either of them to work myself at the moment.

0

精彩评论

暂无评论...
验证码 换一张
取 消