简体   繁体   English

Javascript打印文本区域,文本突出显示颜色

[英]Javascript print textarea with text highlighted colors

I have a textarea that displays a report with keywords like "FAIL", "WARNING", "ERROR". 我有一个textarea,它显示带有“ FAIL”,“ WARNING”,“ ERROR”之类的关键字的报告。 I want to be able to print this textarea with the highlighted colors included. 我希望能够使用包括突出显示的颜色来打印此文本区域。

Currently, I am able to print my textarea using the following function (without highlighted colors): 目前,我可以使用以下功能(没有突出显示的颜色)来打印文本区域:

js: js:

function printTextArea() {
    childWindow = window.open('','childWindow','location=yes, menubar=yes, toolbar=yes');
    childWindow.document.open();
    childWindow.document.write('<html><head></head><body>');
    childWindow.document.write(document.getElementById('textArea').value.replace(/\n/gi,'<br>'));
    childWindow.document.write('</body></html>');

    //this doesn't highlight the text in my print prompt window
    childWindow.document.body.innerHTML.replace(/FAIL/g,"<span class='highlight'>FAIL</span>");

    childWindow.print();
    childWindow.document.close();
    childWindow.close();
}

css: CSS:

body {
  -webkit-print-color-adjust: exact !important;
}

.highlight {
  background-color: yellow;
}

Also, when I view the html before the print() I see the correct class appended to my keyword: 另外,当我在print()之前查看html时,我看到正确的类附加到了关键字上:

<span class='highlight'>FAIL</span>

I'm trying to add the highlight class when writing to the new window and print with highlighted text but it doesn't seem to work. 我正在尝试在写入新窗口时添加突出显示类,并使用突出显示的文本进行打印,但这似乎不起作用。 Is there something I am doing wrong? 我做错什么了吗?

Because the popup window does not include the css defined in the parent window. 因为弹出窗口不包括在父窗口中定义的css。 For easier to debug, better use variable content to store. 为了便于调试,最好使用可变内容进行存储。
To print it with background highlight, only chrome browser works. 要使用背景突出显示进行打印,只能使用chrome浏览器
For more information, please refer to Background color not showing in print preview 有关更多信息,请参阅背景颜色未在打印预览中显示

<html>
<style type="text/css" media="all">
body {
  -webkit-print-color-adjust: exact !important;
}

.highlight {
  background-color: yellow !important;
}
</style>
<script>
function escapeHtml(html){
  var text = document.createTextNode(html);
  var p = document.createElement('p');
  p.appendChild(text);
  return p.innerHTML;
}

function printTextArea() {
    let childWindow = window.open('','childWindow','location=yes, menubar=yes, toolbar=yes');
    childWindow.document.write('<html><head>');
    let styles = document.getElementsByTagName('style');
    for(var i=0; i<styles.length; ++i)
        childWindow.document.write(styles[i].outerHTML);
    childWindow.document.write('</head><body>');
    var content = escapeHtml(document.getElementById('textArea').value).replace(/\n/gi,'<br>');
    content = content.replace(/FAIL/g,"<span class='highlight'>FAIL</span>");
    childWindow.document.write(content);
    childWindow.document.write('</body></html>');

    childWindow.document.close();
    childWindow.focus();
    setTimeout(function () {
        childWindow.print();    
        childWindow.close();
    }, 500);
}
</script>
<body>
<textarea id="textArea">FAIL: ABC
FAIL: DE</textarea>
<button onclick="printTextArea()">print TextArea</button>
</body>
</html>

设置chrome以打印背景

如何访问当前突出显示的文本<textarea>&lt;i&gt;in Javascript&lt;/div&gt;&lt;/i&gt;&lt;b&gt;在Javascript中&lt;/div&gt;&lt;/b&gt;</textarea><div id="text_translate"><p> 用户将在页面上的 HTML 文本区域中输入文本。 当他们突出显示该文本的一部分时,我如何访问突出显示的字符串? 突出显示 textarea 元素中的文本是否会触发任何事件?</p><p> 这是在文本区域是一个组件的反应应用程序的上下文中完成的。</p></div> - How to access the currently highlighted text within a <textarea> in Javascript

暂无
暂无

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

相关问题 在文本区域中显示突出显示的文本 - Displaying highlighted text in textarea 从div获取突出显示的文本并复制到textarea javascript中 - Get highlighted text from div and copy into textarea javascript 使用javascript / jquery在文本区域内用强标签包装突出显示的文本 - Wrap highlighted text within textarea with strong tags using javascript/jquery 如何访问当前突出显示的文本<textarea>&lt;i&gt;in Javascript&lt;/div&gt;&lt;/i&gt;&lt;b&gt;在Javascript中&lt;/div&gt;&lt;/b&gt;</textarea><div id="text_translate"><p> 用户将在页面上的 HTML 文本区域中输入文本。 当他们突出显示该文本的一部分时,我如何访问突出显示的字符串? 突出显示 textarea 元素中的文本是否会触发任何事件?</p><p> 这是在文本区域是一个组件的反应应用程序的上下文中完成的。</p></div> - How to access the currently highlighted text within a <textarea> in Javascript 使用Javascript处理突出显示的文本 - Manipulate highlighted text with Javascript 在Javascript中定位突出显示的文本 - Targeting highlighted text in Javascript 如何将突出显示的文本从textarea提交到数据库 - how to submit highlighted text from textarea into database 在反应 js 中突出显示文本的 textarea 组件 - textarea component with highlighted text in react js 如何从textarea获取突出显示的文本位置? - How to get highlighted text position from textarea? 如何在另一个文本区域中显示文本区域和可编辑 div 中突出显示的文本的 position? - How to show the position of highlighted text in a textarea and editable div, in a another textarea?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM