简体   繁体   English

如何将iframe的格式化html转换为纯文本?

[英]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. 我创建了一个iframe,可以在其中发布格式化的文本(例如,从Word文档中)并以html格式接收。 Is it possible to also recieve the unformatted version (without the html tags), such as created when copying formatted text into a textarea? 是否还可以接收未格式化的版本(没有html标记),例如在将格式化文本复制到文本区域时创建的版本?

My code for logging the html of formatted text: 我用于记录格式化文本html的代码:

<!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. 从技术上讲,我可以要求用户也将格式化的文本粘贴到单独的textarea框中,但是我希望一次完成。

Thanks in advance 提前致谢

Try this var text = editor.body.innerText || editor.body.textContent; 试试这个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 从此答案中获取https://stackoverflow.com/a/6743966/2668119

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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