문제

I have some code that inserts html into a contenteditable as follows:

document.execCommand("insertHTML",false, my_html);

Whats the best way to get a reference to the html element that is created?

도움이 되었습니까?

해결책

Running the command doesn't return you any reference to the inserted content so all you really have available are standard DOM methods. Therefore the easiest way would be to give an ID to the element you want to retrieve. For example:

document.execCommand("insertHTML", false, '<span id="inserted">INSERTED</span>');
var insertedSpan = document.getElementById("inserted");

By the way, the "InsertHTML" command isn't supported in Internet Explorer. You can find cross-browser alternative code here:

https://stackoverflow.com/a/6691294/96100

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top