Html和Internet Explorer:如何避免隐藏的元素被复制/粘贴 [英] Html and Internet Explorer: How to avoid hidden elements to be copied/pasted

查看:364
本文介绍了Html和Internet Explorer:如何避免隐藏的元素被复制/粘贴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在HTML5文档中有一个TABLE。



我有一个视图选择器隐藏/显示一些行,使用jQuery 当用户选择表格(以编程方式)时,隐藏() show()



< ,使用全选按钮,请参阅下面的代码),然后复制/粘贴到例如Word或Outlook电子邮件,该行为不同于FF和IE。




  • FF:不会复制 style ='display:none;'的元素。这是需要和预期的行为

  • IE:复制所有,并粘贴所有,所以我的视觉上的技巧然后对IE用户是无用的。



我添加了我的 selectAll 函数。只需要一个选择器,并选择其中的所有文本。这是我从jQuery论坛复制/粘贴的东西。

  jQuery.fn.selectText = function(){
var doc = document;
var element = this [0];
// console.log(this,element);
if(typeof element =='undefined'){
return;
}
if(doc.body.createTextRange){
var range = document.body.createTextRange();
range.moveToElementText(element);
range.select();
} else if(window.getSelection){
var selection = window.getSelection();
var range = document.createRange();
range.selectNodeContents(element);
selection.removeAllRanges();
selection.addRange(range);
}
};

如何只选择可见元素?



BTW 我会downvote谁会建议: / p>


  • 更改用户


  • 强制用户更改浏览器




class =h2_lin>解决方案

似乎这个问题在多次之前被问过。 (1) - ( 2



似乎没有一个响应适合所有人。



您可以使用是。(':可见')在jQuery,但不确定翻译回到javascript(不是很好)。



其他人建议克隆表格,只复制其他表格中隐藏的内容,但随后会导致其他浏览器出现问题。 / p>

你可以使用上面的方法,但它是一个讨厌的黑客。



href =http://stackoverflow.com/a/10130156/2471423>这里您可能会感兴趣,但我不知道你想要复制,如果这是值得的努力。



< joke>





  • 仅显示可见单元格和更改服务器代码

    ul>

    < / joke>


    I've a TABLE, in a HTML5 document.

    I've a view selector who hide/show some rows, using jQuery hide() and show().

    When user select the table (programmatically, using a 'select all' button, see below for the code), and then copy/paste into, for example, Word or Outlook email, the behaviour is different from FF and IE.

    • FF: doesn't copy elements with style='display: none;'. It's desired and expected behaviour
    • IE: copy ALL, and paste ALL, so my visual 'trick' is then useless for IE users.

    I add my selectAll function. Simply expect a selector and select all the text inside it. It's something I copy/pasted from the jQuery forum.

    jQuery.fn.selectText = function(){
        var doc = document;
        var element = this[0];
        // console.log(this, element);
        if (typeof element == 'undefined') {
            return;
        }
        if (doc.body.createTextRange) {
            var range = document.body.createTextRange();
            range.moveToElementText(element);
            range.select();
        } else if (window.getSelection) {
            var selection = window.getSelection();        
            var range = document.createRange();
            range.selectNodeContents(element);
            selection.removeAllRanges();
            selection.addRange(range);
        }
    };
    

    How can I select only visible elements ? Or... is there any other html/js trick?

    BTW I'll downvote whoever will propose to:

    • change users

    • force users to change browser

    • ask me to render only visible cells and change server code

    解决方案

    It seems like this question has been asked before multiple times. (1) - (2)

    It seems like there has not been a response that works fine for everyone.

    You could use is.(':visible') in jQuery but not sure that translates back to javascript (not very good at it).

    Other people have suggested cloning the table and only copying what's in the other table which is hidden, but then that causes a problem later on with other browsers which work fine.

    You could use the above method which would work but it's a nasty hack.

    There is an answer here which may be of interest to you, however I'm not sure what you're trying to copy and if this is worth the effort.

    <joke>

    If all else fails, here are some other recommendations:

    • change users

    • force users to change browser

    • render only visible cells and change server code

    </joke>

    这篇关于Html和Internet Explorer:如何避免隐藏的元素被复制/粘贴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆