chrome добавляет возврат каретки с помощью execCommand('copy')

Когда я использую document.execCommand('copy'), chrome добавляет возврат каретки в конце скопированного текста (которого на самом деле нет в HTLM, а в IE нет (правильное поведение). Я делаю что-то не так?

   function copycode(){

    var length=this.id.length;
    var preid = this.id.substring(0,length-1);
    var textnode=document.getElementById(preid);
    textnode.setAttribute('contenteditable', 'true');
    window.getSelection().removeAllRanges();
    var range = document.createRange();  
    range.selectNode(textnode);
    window.getSelection().addRange(range);
    var succeed;
    try {
      succeed = document.execCommand("copy");
    } 
    catch(e) {
      succeed = false;
    }
    textnode.setAttribute('contenteditable', 'false');

}


person Nancymic    schedule 16.05.2016    source источник


Ответы (1)


Проблема заключается не в выполнении команды копирования, "document.execCommand('copy')", это работает нормально. Выбор диапазона - проблема.

Я столкнулся с той же проблемой и решил ее, используя: element.SELECT(). Например:

Создайте текстовое поле и поместите его за пределы экрана (скрытие не сработает). Установите значение и выберите всю текстовую область.

    var textarea = document.createElement( "textarea" );
    textarea.style.height = "0px";
    textarea.style.left = "-100px";
    textarea.style.opacity = "0";
    textarea.style.position = "fixed";
    textarea.style.top = "-100px";
    textarea.style.width = "0px";
    document.body.appendChild( textarea );

    textarea.value = textnode.innerHTML;
    textarea.select();

    document.execCommand('copy');
person Gerard Verbeek    schedule 21.03.2017