简体   繁体   English

使用Javascript将HTML转换为纯文本

[英]Using Javascript to convert HTML to Plaintext

Using code found here: What is the most convenient way to convert HTML to plain text while preserving line breaks (with JavaScript)? 使用此处提供的代码: 在保留换行符的同时将HTML转换为纯文本的最便捷方法是什么(使用JavaScript)?

I need to be able to convert HTML to plaintext using Javascript, via a button that will update the text displayed depending on user input 我需要能够使用Javascript通过一个按钮将HTML转换为纯文本,该按钮将根据用户输入更新显示的文本

The code that I'm using: 我正在使用的代码:

<div id="content">
<p>
    Here is a paragraph with a line break in it
    <br>
    Second line
</p>
<p>
Operating System:
          <select id='OperatingSystem'>
            <option value='Windows10'>Windows 10</option>
            <option value='Windows8.1'>Windows 8.1</option>
            <option value='Windows8'>Windows 8</option>
            <option value='Windows7'>Windows 7</option>
            <option value='WindowsVista'>Windows Vista</option>
            <option value='WindowsXP'>Windows XP</option>
            <option value='Mac10.12'>Mac 10.12 Sierra</option>
            <option value='Mac10.11'>Mac 10.11 El Capitan</option>
            <option value='Mac10.10'>Mac 10.10 Yosemite</option>
            <option value='Mac10.9'>Mac 10.9 Mavericks</option>
            <option value='Mac10.8'>Mac 10.8 Mountain Lion</option>
            <option value='Mac10.7'>Mac 10.7 Lion</option>
            <option value='Android'>Android</option>
            <option value='iOS'>iOS</option>
            <option value='Other'>Other</option>
          </select>
    <br/>Alternate Phone:
          <input value="Boogers" type="text">
</p>
</div>
<!-- Submit -->
<input type="submit" value="submit" onclick="getInnerText();" />
<textarea id="text" rows="6" cols="50"></textarea>
<!-- Function -->
<script>
function getInnerText(el)
{
var sel, range, innerText = "";
if (typeof document.selection != "undefined" && typeof document.body.createTextRange != "undefined") {
    range = document.body.createTextRange();
    range.moveToElementText(el);
    innerText = range.text;
} else if (typeof window.getSelection != "undefined" && typeof document.createRange != "undefined") {
    sel = window.getSelection();
    sel.selectAllChildren(el);
    innerText = "" + sel;
    sel.removeAllRanges();
}
return innerText;
}

document.getElementById("text").value = getInnerText(document.getElementById("content"));
</script>
<!-- Tabs Javascript - End -->

jsfiddle sample jsfiddle示例
Unfortunately, either the button isn't working, or the code will not read the user input (dropdown box and text input). 不幸的是,该按钮不起作用,或者代码无法读取用户输入(下拉框和文本输入)。 How can I fix this? 我怎样才能解决这个问题?

The problem is that your current method of extracting the text wont get the values of inputs. 问题是您当前提取文本的方法将无法获取输入值。 You'll have to fetch and reinsert those manually: 您必须手动获取并重新插入它们:

var os = document.getElementById('OperatingSystem').value;
var phone = document.getElementById('phone-input').value;
innerText = innerText.replace('Alternate Phone:', 'Alternate Phone: ' + phone);
innerText = innerText.replace('Operating System:', 'Operating System: ' + os);

I've updated your fiddle with a solution that does that. 我已经用一种解决方法更新了您的小提琴。

https://jsfiddle.net/hnzck5nt/6/ https://jsfiddle.net/hnzck5nt/6/

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

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