gpt4 book ai didi

javascript - event.clipboardData 未定义

转载 作者:行者123 更新时间:2023-12-03 22:59:28 24 4
gpt4 key购买 nike

我正在尝试访问浏览器中的粘贴事件并覆盖它。但是 event.clipboardData 未定义。目前我所拥有的就是这个:

function handlePaste (event) {

event.preventDefault();

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

编辑:

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

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:

<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>

最佳答案

我最近遇到了类似的问题,我试图拦截粘贴事件。我使用的是 here 中的代码:

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 行更改为

clipboardData = event.clipboardData || window.clipboardData || event.originalEvent.clipboardData;

现在一切正常。

关于javascript - event.clipboardData 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30257962/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com