Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在将粘贴复制到contenteditable div时,在chrome上添加了额外的div_Javascript_Jquery_Html_Google Chrome_Contenteditable - Fatal编程技术网

Javascript 在将粘贴复制到contenteditable div时,在chrome上添加了额外的div

Javascript 在将粘贴复制到contenteditable div时,在chrome上添加了额外的div,javascript,jquery,html,google-chrome,contenteditable,Javascript,Jquery,Html,Google Chrome,Contenteditable,我有contenteditable div我只想要原始文本而不是HTML内容,同时从任何网站复制粘贴。我找到了一些解决方案,并尝试了以下方法 $('[contenteditable]').on('paste',function(e) { e.preventDefault(); var text = (e.originalEvent || e).clipboardData.getData('text/plain') || prompt('Paste something..');

我有contenteditable div我只想要原始文本而不是HTML内容,同时从任何网站复制粘贴。我找到了一些解决方案,并尝试了以下方法

$('[contenteditable]').on('paste',function(e) {
    e.preventDefault();
    var text = (e.originalEvent || e).clipboardData.getData('text/plain') || prompt('Paste something..');
    window.document.execCommand('insertText', false, text);
});
它工作正常,但问题是它在chrome浏览器中添加了额外的div


有没有更好的解决办法?谢谢

我也面临着同样的问题,有时还会增加&etc。我使用下面的正则表达式删除所有html标记。 调用blur事件时,在函数内部使用以下代码删除标记。即使您将内容复制并粘贴到可编辑的div中,它也可以工作

x = x.replace(/(<[^\\>]*>|<br\/>|&amp;|amp;|&lt|&gt;)/g, '');
x = x.trim();
x=x.replace(/(]*>| | | |和| |< |)/g';
x=x.trim();
希望这是您正在寻找的

我们可以使用jquery的.text()函数删除所有HTML标记

$('[contenteditable]').on('paste',function(e) {
    e.preventDefault();
    var text = $(this).text();

    $(this).text(text);
});