复制到contenteditable <span> </span>

时间:2012-01-25 05:58:48

标签: html5 firefox contenteditable html

我需要复制span标记<span contenteditable="true">//content</span>中的内容 但它没有在FireFox中实现,有什么解决方案吗? 这是我的范围http://jsfiddle.net/watxD/

2 个答案:

答案 0 :(得分:1)

一个。 var Result = $('span[contenteditable="true"]').text();

B中。 var Result = $('span[contenteditable="true"]').html();

℃。内部节点列表:var Result = $('span[contenteditable="true"]').contents();

d。您可以使用Rangy Library http://code.google.com/p/rangy/来完成此操作。 (它使用FF,Chrome,Opera,IE9的本机方法,IE的非本地方法&lt; = 8) 下面的代码使用的是jQuery - 但您可以在纯节点Javascript

上重写它
  1. 您需要为您的节点创建范围

    var Range = rangy.createRange();
    
  2. 您需要选择节点的内容

    Range.selectNodeContents( $('span[contenteditable="true"]')[0] )
    
  3. 根据需要表示结果:

    var Result = Range.toString(); // Returns the text contained within the range.
    
    var Result = Range.toHtml(); // Returns a string containing an HTML representation of the range. 
    

答案 1 :(得分:0)

在Firefox中没有纯JavaScript方式可以做到这一点,而不需要用户搞乱他们的偏好。允许从JavaScript一般访问系统剪贴板是危险的,因此用户可能不希望这样做。这篇文章概述了问题以及如何在Firefox中启用剪贴板访问。我不知道它是否是最新的。

http://kb.mozillazine.org/Granting_JavaScript_access_to_the_clipboard

另一个选项是基于Flash的hacky解决方法,例如ZeroClipboard

相关问题