首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >event.clipboardData未定义

event.clipboardData未定义
EN

Stack Overflow用户
提问于 2015-05-15 11:07:09
回答 1查看 15.4K关注 0票数 9

我正在尝试访问浏览器中的粘贴事件并覆盖它。然而,event.clipboardData是未定义的。目前我所掌握的是:

代码语言:javascript
复制
function handlePaste (event) {

    event.preventDefault();

    console.log("Handling paste");
    console.log(event.clipboardData);
}

编辑:

它是角指令的一部分,我在Chrome中运行它:

代码语言:javascript
复制
app.directive('safePaste', [function() {

function handlePaste (event) {

    event.preventDefault();

    console.log("Handling paste");
    console.log(event.clipboardData);
}

/*
 * Declaration
 */
var declaration = {};

declaration.restrict = 'A';

declaration.link = function(scope, element, attr) {
    // Attach the paste handler
    element.on('paste', handlePaste);

    // Register to remove the paste handler
    scope.$on('$destroy', function() {
        element.off('paste', handlePaste);
    });
};

return declaration;
} 
]);

HTML:

代码语言:javascript
复制
<li ng-repeat="note in notes | reverse">
     <a id="note" href="#">
        <h2 id="note-title" data-note-id="{{ note.id }}" safe-paste> {{ note.title | limitTo : 16 }}</h2>
            <p id="note-content" data-note-id="{{ note.id }}" safe-paste> {{ note.text | limitTo : 200 }} </p>
            <p id="info-note-save" hidden="true" class="text-center">Press enter to save</p>
     </a>
</li>
EN

回答 1

Stack Overflow用户

发布于 2017-05-18 03:03:54

最近我遇到了一个类似的问题,我试图拦截粘贴事件。我在使用来自here的代码

代码语言:javascript
复制
function handlePaste (e) {
    var clipboardData, pastedData;

    // Get pasted data via clipboard API
    clipboardData = e.clipboardData || window.clipboardData;
    pastedData = clipboardData.getData('Text').toUpperCase();

    if(pastedData.indexOf('E')>-1) {
        //alert('found an E');
        e.stopPropagation();
        e.preventDefault();
    }
};

我将clipboardData行改为

代码语言:javascript
复制
clipboardData = event.clipboardData || window.clipboardData || event.originalEvent.clipboardData;

现在一切都好了。

票数 19
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30257962

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档