【问题标题】:Adding an onpaste event to a dijit/Editor将 onpaste 事件添加到 dijit/Editor
【发布时间】:2018-05-29 06:54:03
【问题描述】:

我有一个包含大量以声明方式创建的数字/编辑器的应用程序。我需要向这些编辑器添加一个 onpaste 事件,以便在粘贴之前将粘贴的内容转换为纯文本。我无法触发事件。我尝试将事件作为 data-dojo-props 中的组件和单独的 data-dojo-attach-event 属性附加。两者似乎都不起作用。

以下是其中一个字段的示例:

<div  data-dojo-type="dijit/Editor" id="Editor1" name="Editor1Content"  
data-dojo-props="extraPlugins:
['createLink','unlink','fontSize','foreColor','hiliteColor'], 
onChange:function(){MarkDocAsChanged();}" data-dojo-attach-
event="onPaste:function(){pasteAsPlainText(event);}" >This is the current 
field content</div>

谁能指出我正确的方向?

【问题讨论】:

  • 您是否忘记在代码中添加“dojox.editor.plugins.SafePaste”?默认情况下,编辑器没有 onPaste 事件。见这里dojotoolkit.org/api
  • 你是说如果我包含 SafePaste 插件我就可以使用 onpaste 吗?还是我需要以某种方式使用 SafePaste 插件来调用我的函数?

标签: javascript dojo onpaste


【解决方案1】:

查看 dijit/Editor 文档,它似乎不支持 onPaste 事件。您可以尝试将 onpaste 侦听器附加到 widget.domNode,拦截事件,并在那里转换值,然后将其设置为 widget.value。

【讨论】:

  • 这样的事情似乎是唯一的途径。考虑到应用程序中有多少编辑器,我希望避免它。
【解决方案2】:
// try to registe the paste event with "dojo/on" on the domNode
on(target, "paste", function(event){
              var textClipboard = "";
              if (typeof event.clipboardData !== "undefined") {// on Chrome & FF
                  textClipboard = event.clipboardData.getData("text/plain");
              } else if (typeof clipboardData !== "undefined") { // IE
                  textClipboard = clipboardData.getData("text");
              }
// use document.execCommand("insertText", false, text) or
// document.execCommand("paste", false, text) in IE to paste content
          });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-27
    • 2017-06-17
    • 2022-09-28
    • 2012-02-17
    • 2016-11-01
    • 1970-01-01
    • 2012-05-28
    相关资源
    最近更新 更多