“display:none” content copied to clipboard, visible when pasted

后端 未结 5 1703
星月不相逢
星月不相逢 2020-12-31 13:37

I\'m having a problem with non-displayed HTML elements being copied to the clipboard, and then displayed when the content is pasted into MS Word, Outlook, etc.

For e

相关标签:
5条回答
  • 2020-12-31 14:07

    Use type='hidden' instead of type='text' for the input box and wrap this inside a div with style set to display: none

    0 讨论(0)
  • 2020-12-31 14:11

    Here is the solution I used to work around it.

    The strategy:

    1. generate a unique number when you remove an element
    2. replace element in the DOM with a new div (aka: the replacer div) with an ID which we will be able to find given that we know the unique number generated in the last step.
    3. add a property to the element so that when we see it later we can extract the unique number
    4. move the element into a div which is declared in a variable to remove it from the document completely.
    5. When we want to move the element back we simply get the unique number from the property, locate the replacer div we left behind and replace it with the original element.

    Here are some notes:

    1. I used slideUp() and slideDown() to animate the removal, but you can replace those calls as you see fit.
    2. I put the elements in a div element in a variable. You could choose to move it somewhere else in the DOM, but I wanted it completely removed. You could also just put it in a variable or an array. The reason I used a div variable is I wanted to be able to use jQuery's DOM search on it, but I didn't want it in the DOM. For example, I can do: whereHiddenThingsLive.find('.some-class').

    The code:

    var whereHiddenThingsLive = $('<div></div>');
    var nextNum = 0;
    
    function hideElement(element) {
        if (element.hasClass('sop-showing')) {
            element.finish();
        }
        if (element.is(':hidden') || element.hasClass('sop-hiding')) return;
        var num = nextNum++;
        element.addClass('sop-hiding');
        element.slideUp(400, function () {
            var replacer = $('<div class="hide-replacer" style="display:none;"></div>').prop('id', 'hide-replacer-' + num);
            element.prop('replaced-by', num);
            element.after(replacer);
            element.appendTo(whereHiddenThingsLive);
            element.removeClass('sop-hiding');
        });
    }
    
    function showElement(element) {
        if (element.hasClass('sop-hiding')) {
            element.finish();
        }
        if (element.is(':visible') || element.hasClass('sop-showing')) return;
        element.addClass('sop-showing');
        var num = element.prop('replaced-by');
        element.detach();
        element.removeProp('replaced-by');
        $('#hide-replacer-' + num).after(element).remove();
        element.slideDown(400, function() {
            element.removeClass('sop-showing');
        });
    }
    
    0 讨论(0)
  • 2020-12-31 14:19

    It sounds like you need to have the JavaScript create the DOM sections rather than just changing CSS styles. Instead of changing the display property of the "I'm hidden" paragraph, have the JavaScript create that element when you want it to display, and remove it whan you want to hide it.

    If the elements are complicated enough, then perhaps you can have them at the bottom of the document with "display:none", but then move them into the place where you want them visible.

    0 讨论(0)
  • 2020-12-31 14:21

    If you require users to copy content, I'd recommend dropping that content in a <textarea /> and allowing them to select/copy by clicking a button. It can be difficult to select exactly the right text in browsers.

    0 讨论(0)
  • 2020-12-31 14:24

    You should be aware that hiding HTML with CSS only works if the renderer supports all the CSS styles.

    Just because you do not see copy/pasted HTML in Outlook does not mean the data is not already there.

    I am not sure what you are actually trying to achieve: Why do you need your users to copy HTML into Outlook at all?

    0 讨论(0)
提交回复
热议问题