【问题标题】:Using Javascript to convert HTML to Plaintext使用 Javascript 将 HTML 转换为纯文本
【发布时间】:2017-02-03 16:53:07
【问题描述】:

使用此处找到的代码:What is the most convenient way to convert HTML to plain text while preserving line breaks (with JavaScript)?

我需要能够使用 Javascript 将 HTML 转换为纯文本,通过一个按钮,该按钮将根据用户输入更新显示的文本

我正在使用的代码:

<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
很遗憾,按钮不起作用,或者代码无法读取用户输入(下拉框和文本输入)。 我该如何解决这个问题?

【问题讨论】:

  • 首先查看浏览器控制台中抛出的错误

标签: javascript html button plaintext


【解决方案1】:

问题是您当前提取文本的方法无法获取输入值。您必须手动获取并重新插入它们:

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);

我已经用一个解决方案更新了你的小提琴。

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

【讨论】:

  • 嗨 Schlaus,我更新了你的小提琴,因为 OP 的 onclick 函数也在调用 getInnerText() 时没有参数,因此引发了错误。请参阅jsfiddle.net/hnzck5nt/6 - 如果您想编辑您的答案
  • @Booboobeaker 谢谢,没有打开控制台,所以我没有注意到错误。已更新到您的版本!
猜你喜欢
  • 2017-03-20
  • 1970-01-01
  • 2011-01-25
  • 2022-01-25
  • 1970-01-01
  • 1970-01-01
  • 2012-04-12
  • 2014-09-08
  • 2013-10-14
相关资源
最近更新 更多