admin管理员组

文章数量:1336357

I have little problem with copying text. On my website color of the font is set to white:

body {
    color: #FFF;
}

Exmaple:

When I copy for example "List of programs" and I want paste it to Word, Lync text is white. Is it possible to add some styles/js which change this color to black in external programs? I know in word is paste option "Keep text only" but what with Lync?

@Update

That javascript works almost I expected. Problem is with IE. Any idea?(tested on chrome 45.0.2454.101)

 (function (container, defaultColor, copyColor) {
    selectedText = window.getSelection();

    $(container).keydown(function (e) {
        e = e || window.event;
        var key = e.which || e.keyCode;
        var ctrl = e.ctrlKey ? e.ctrlKey : ((key === 17) ? true : false);

        if (key == 67 && ctrl) {
            var range = getRange(selectedText);
            changeColor(range, selectedText, copyColor);
        }
    }).keyup(function (e) {
        var range = getRange(selectedText);

        if (range) {
            selectedText.removeAllRanges();
            selectedText.addRange(range);
        }
        changeColor(range, selectedText, defaultColor);
    });

    function getRange(text) {
        if (text.rangeCount && text.getRangeAt) {
            return text.getRangeAt(0);
        }
        return null;
    }

    function changeColor(range, selectedText, color) {
        document.designMode = "on";

        if (range) {
            selectedText.removeAllRanges();
            selectedText.addRange(range);
        }
        document.execCommand("ForeColor", false, color);
        document.designMode = "off";
    }
})("body", "white", "black");

I have little problem with copying text. On my website color of the font is set to white:

body {
    color: #FFF;
}

Exmaple:

When I copy for example "List of programs" and I want paste it to Word, Lync text is white. Is it possible to add some styles/js which change this color to black in external programs? I know in word is paste option "Keep text only" but what with Lync?

@Update

That javascript works almost I expected. Problem is with IE. Any idea?(tested on chrome 45.0.2454.101)

 (function (container, defaultColor, copyColor) {
    selectedText = window.getSelection();

    $(container).keydown(function (e) {
        e = e || window.event;
        var key = e.which || e.keyCode;
        var ctrl = e.ctrlKey ? e.ctrlKey : ((key === 17) ? true : false);

        if (key == 67 && ctrl) {
            var range = getRange(selectedText);
            changeColor(range, selectedText, copyColor);
        }
    }).keyup(function (e) {
        var range = getRange(selectedText);

        if (range) {
            selectedText.removeAllRanges();
            selectedText.addRange(range);
        }
        changeColor(range, selectedText, defaultColor);
    });

    function getRange(text) {
        if (text.rangeCount && text.getRangeAt) {
            return text.getRangeAt(0);
        }
        return null;
    }

    function changeColor(range, selectedText, color) {
        document.designMode = "on";

        if (range) {
            selectedText.removeAllRanges();
            selectedText.addRange(range);
        }
        document.execCommand("ForeColor", false, color);
        document.designMode = "off";
    }
})("body", "white", "black");
Share Improve this question edited Jan 10, 2017 at 13:45 Mroczny Arturek asked Jan 10, 2017 at 9:15 Mroczny ArturekMroczny Arturek 4191 gold badge8 silver badges24 bronze badges 2
  • Paste first to notepad to clear any formatting and then from notepad to Lync. – VadimB Commented Jan 10, 2017 at 9:20
  • Yeah, it works. But it's only temporary solution. – Mroczny Arturek Commented Jan 10, 2017 at 9:30
Add a ment  | 

6 Answers 6

Reset to default 3

This code solved my problem.

window.onload = function () {
document.addEventListener('copy', function (e) {
    selectedText = window.getSelection().toString();
    if (window.clipboardData) {
        window.clipboardData.setData("Text", selectedText);
    } else {
        e.clipboardData.setData('text/plain', selectedText);
    }

    e.preventDefault(); 
});}

You can copy paste the link from anywhere, then in your Microsoft word select the text that you have pasted and then right-click -> paste options -> merge formatting. I think this would solve your problem.

You can reset text while selecting as follows:

p.reset_selection {
  color: #FFF;
  background-color: #31727E;
  padding: 15px;
}
p.reset_selection::-moz-selection {
  background-color: #FFF;
  color: #000;
}
p.reset_selection::selection {
  background-color: #FFF;
  color: #000;
}
<p class="reset_selection">List of programs</p>

Or you can paste text in Lync using Ctrl+Shift+V

Or you can try PureText using which you can configure a hotkey like Win+V to paste text only without any styling.

To get color of copied text as black, you have to add following css to your code:

::-moz-selection { /* For Firefox */
    color: #000;
}

::selection {
    color: #000;
}

It will change the color of the text you are selecting to #000 i.e., black and the text then you copied have black color.

I believe you are looking for ::selection. You can use it to set the color of the text or the background of the selected area (default is blue in chrome and some other browsers :-) )

::-moz-selection {
   background-color: #FFA;
   color: #000;
}

/* Works in Safari */

::selection {
   background-color: #FFA;
   color: #000;
}

本文标签: javascriptHow to change color of copied textStack Overflow