gpt4 book ai didi

javascript - 即使在 Angular Directive(指令)中使用preventDefault(),Chrome也会在拖动时加载图像

转载 作者:行者123 更新时间:2023-12-02 17:10:16 24 4
gpt4 key购买 nike

我遇到了 Angular Directive(指令)问题,该指令无法阻止默认的 chrome 操作。

这是我的指令

app.directive('fileDrag', function () {
return {
restrict: 'A',
link: function (scope, elem, attrs) {
elem.bind('drop', function(e){
e.preventDefault();
console.log(e);
return false;
});
}
};
});

还有我的 html

<div style="width: 500px; height:500px; background-color: red;" file-drag></div>

当拖动到元素上时,我似乎无法让 Chrome 不在新选项卡中加载图像。有什么想法吗?

最佳答案

您还应该在 dragover 事件上 preventDefault,因此将代码更改为:

app.directive('fileDrag', function () {
return {
restrict: 'A',
link: function (scope, elem, attrs) {
elem.bind('drop', function(e){
e.preventDefault();
console.log(e);
return false;
});
elem.bind('dragover', function(e){
e.preventDefault();
});
}
};
});

关于javascript - 即使在 Angular Directive(指令)中使用preventDefault(),Chrome也会在拖动时加载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24876832/

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