简体   繁体   中英

Convert contenteditable div's content to plaintext via javascript

I'm trying to make custom lightweight rich text editor with just one feature - adding links. I did some research and desided iframe is the best choice. After some messing around it works with one exception - I need to run some code on keyup event. I read everything I found on the internet and nothing helped, still doesn't work...

iframe.document.designMode = 'On';
iframe.document.open();
iframe.document.write(someHTML);
iframe.document.close();

var keyupHandle = function() { /* some code */ };

var iframeDoc = document.getElementById('iframe').contentWindow.document;
if(iframeDoc.addEventListener) {
    iframeDoc.addEventListener('keyup', keyupHandle(), true);
} else {
    iframeDoc.attachEvent('onkeyup', keyupHandle());
}

I think I remember needing to wait for the iframe to fully load before adding event handlers to the document. If possible, add something to the iframe's HTML to call out to the parent page when it's loaded:

window.iframeLoaded = function() {
    var iframeDoc = document.getElementById('iframe').contentWindow.document;
    if(iframeDoc.addEventListener) {
        iframeDoc.addEventListener('keyup', keyupHandle(), true);
    } else {
        iframeDoc.attachEvent('onkeyup', keyupHandle());
    }
};

iframe.document.designMode = 'on';
iframe.document.open();
iframe.document.write('<html><body onload="parent.iframeLoaded()">Stuff</body></html>');
iframe.document.close();

Failing that, setting a brief timer using window.setTimeout() will probably work.

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