简体   繁体   中英

How to get/convert formatted html of iframe into plain text?

I created an iframe into which I can post formatted text (from a Word document for example) and receive it as html. Is it possible to also recieve the unformatted version (without the html tags), such as created when copying formatted text into a textarea?

My code for logging the html of formatted text:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>

    <script>
        window.addEventListener("load", function () {
            var editor = theWYSIWYG.document;
            editor.designMode = 'on';
            action.addEventListener('click', function() {
                var formattedHTML = editor.body.innerHTML
                console.log(formattedHTML);
            }, false)
        }, false)
    </script>
</head>
<body>
    <div id="textEditor">
        <button id="action" title="Bold"><b>Click me</b></button>
        <div id="richTextArea"></div>
            <iframe id="theWYSIWYG" name="theWYSIWYG" frameborder="0"></iframe>
    </div>
</body>
</html>

Technically I could ask the user to also paste the formatted text into a seperate textarea box, but I would prefer to do it in a single time.

Thanks in advance

Try this var text = editor.body.innerText || editor.body.textContent; var text = editor.body.innerText || editor.body.textContent;

Taken from this answer https://stackoverflow.com/a/6743966/2668119

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