event.clipboardData is undefined
我正在尝试在浏览器中访问粘贴事件并覆盖它。 但是event.clipboardData是未定义的。 目前我所拥有的是:
1 2 3 4 5 6 7 | function handlePaste (event) { event.preventDefault(); console.log("Handling paste"); console.log(event.clipboardData); } |
编辑:
它是Angular中指令的一部分,我正在Chrome中运行它:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | 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:
1 2 3 4 5 6 7 8 9 10 | <li ng-repeat="note in notes | reverse"> <h2 id="note-title" data-note-id="{{ note.id }}" safe-paste> {{ note.title | limitTo : 16 }} <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> </li> |
我最近有一个类似的问题,我尝试拦截粘贴事件。 我在这里使用代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 | 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(); } }; |
我改为将剪贴板数据行改为
1 | clipboardData = event.clipboardData || window.clipboardData || event.originalEvent.clipboardData; |
现在一切正常。