简体   繁体   English

复制带有颜色和样式的文本

[英]Copy text with color and style

This is the JavaScript code to copy text:这是复制文本的 JavaScript 代码:

$(function() {
  $("#copyAndopenOutlook").click(function(e) {
    e.preventDefault();
    // Code below
    
    var newLine = "\n";
    
    var emailBodyText = "";
    
    emailBodyText = emailBodyText + "This line I want bold" + newLine;
    emailBodyText = emailBodyText + "This is just another line for practice" + newLine + newLine;
    emailBodyText = emailBodyText + "This is the last line, I want it green color";
    
    
    const el = document.createElement('textarea');
    el.value = emailBodyText;
    el.setAttribute('readonly', '');
    el.style.position = 'absolute';
    el.style.left = '-9999px';
    document.body.appendChild(el);
    el.select();
    document.execCommand('copy');
    document.body.removeChild(el);
    
    // alert("Text is copied, and can now be pasted into outlook new mail");
    
    var mail = document.createElement("a");
    mail.href = "mailto:someone@example.com?subject=Test ";
    mail.click();
    
    // Code Above
    });
});

I want to copy this and paste it in Outlook, that I have working, but how can I make the first line bold, and last line green color?我想复制它并将其粘贴到我正在工作的 Outlook 中,但是如何使第一行加粗,最后一行变为绿色? I have tried with adding <b> in the code, and also for color, but it just copies the and color tags.我尝试在代码中添加<b>以及颜色,但它只是复制了和颜色标签。

If you want to mark up the text, you need an element that supports HTML content.如果要标记文本,则需要一个支持 HTML 内容的元素。 For instance, here's a version using a div and the Selection object .例如,这是一个使用divSelection object的版本。

 $(function() { $("#copyAndopenOutlook").click(function(e) { e.preventDefault(); // Code below const newLine = "\n"; let emailBodyText = ""; emailBodyText = emailBodyText + "<strong>This line I want bold</strong>" + newLine; emailBodyText = emailBodyText + "This is just another line for practice" + newLine + newLine; emailBodyText = emailBodyText + "<span style='color: green'>This is the last line, I want it green color</span>"; const el = document.createElement('div'); el.innerHTML = emailBodyText; el.style.position = 'absolute'; el.style.left = '-9999px'; document.body.appendChild(el); const sel = getSelection(); sel.setBaseAndExtent(el, 0, el, el.childNodes.length); document.execCommand('copy'); document.body.removeChild(el); alert("Text is copied, and can now be pasted into outlook new mail"); /* var mail = document.createElement("a"); mail.href = "mailto:someone@example.com?subject=Test "; mail.click(); */ // Code Above }); });
 <input type="button" id="copyAndopenOutlook" value="Copy"> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Some truly obsolete browsers may not support the Selection object, but anything modern will.一些真正过时的浏览器可能不支持Selection object,但任何现代浏览器都可以。

You may need <br> instead of \n , or if the "lines" are actually paragraphs, perhaps <p>...</p> .您可能需要<br>而不是\n ,或者如果“行”实际上是段落,也许是<p>...</p>

or if you want to do it with Clipboard API:或者如果你想用 Clipboard API 来做:

  const type = "text/html";
  const text = `<p style="color:red">this is in red</p>`;
  const blob = new Blob([text], { type });
  const data = [new ClipboardItem({ [type]: blob })];

  navigator.clipboard.write(data).then(
    () => {
      /* success */
    },
    () => {
      /* failure */
    }
  );

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

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