为什么document.execCommand(" copy")不再在Internet Explorer 11中运行?

时间:2017-12-20 13:47:04

标签: javascript html clipboard

在我们的应用程序中,我们使用以下逻辑将HTML(文本和格式)复制到剪贴板。



Options -> Security -> Edit level ... -> Scripting -> Allow access to clipboard

#include <algorithm>
#include <functional>
#include <iterator>
#include <random>
#include <vector>
#include <iostream>

int main() {
    using int_vec_t = std::vector<int>;
    std::vector<int_vec_t> v = {
        {0, 1, 2}, {}, {}, {3, 4, 5},
        {}, {6, 7, 8}, {}, {}, {9}, {10, 11}
    };

    // You can't put reference direcly, so use reference_wrapper instead
    std::vector<std::reference_wrapper<int_vec_t> > nonempty;
    nonempty.reserve(v.size());
    // "copy" non empty vectors. (Doesn't do copy, actually)
    std::copy_if(v.begin(), v.end(), std::back_inserter(nonempty), [](const int_vec_t& v) { return !v.empty();});
    if (nonempty.empty())
        return 0;
    // pick an element
    static std::random_device rd;
    static std::mt19937 gen(rd());
    std::uniform_int_distribution<> dis(0, nonempty.size() - 1);
    const int_vec_t& result = nonempty[dis(gen)];
    // dump result
    std::copy(result.begin(), result.end(), std::ostream_iterator<int>(std::cout, ", "));

    return 0;
}
&#13;
&#13;
&#13;

它在所有主要浏览器(Chrome,Firefox,Edge和Internet Explorer)中运行良好。

使用最新的Internet Explorer版本11.125.16299.0(Updateversion:11.0.49 - KB4052978),HTML不再复制到剪贴板。

在以下情况下有一个安全设置:

 with open(filename, 'r') as f_in:
        # Use the csv library to set up a DictReader object.
        trip_reader = csv.DictReader(f_in)
        # Use a function on the DictReader object to read the
        # first trip from the data file and store it in a variable.
        for row in trip_reader:
                   pprint(row)

我更改了#34; Ask&#34;到&#34;激活&#34;。这没有效果。

有人知道为什么,他们改变了什么,也许是另一种解决方案或解决方法?谢谢。

1 个答案:

答案 0 :(得分:3)

事实证明问题不是document.execCommand("copy"),而是document.execCommand('selectAll',false,null)。虽然它可视地选择div的内容(您可以看到它,如果不从DOM中删除它),复制命令无法识别选择。

以下代码有效:

function copy(element_id)
{
    var aux = document.createElement("div");
    aux.setAttribute("contentEditable", true);
    aux.innerHTML = document.getElementById(element_id).innerHTML;
    document.body.appendChild(aux);
    window.getSelection().selectAllChildren(aux);
    document.execCommand("copy");
    document.body.removeChild(aux);
    console.log("COPY");
}
<p id="demo"><b>Bold text</b> and <u>underlined text</u>.</p>
  
<button onclick="copy('demo')">Copy To Clipboard Keeping Format</button>