我正在尝试使用 cropper plugin by fengyuanchen但是遇到了一个不可能的错误
我经常遇到 Uncaught TypeError: canvas.cropper
is not a function 即使我已经尝试了我在你的指南中搜索的所有内容甚至问题但我无法解决它。我尝试在单击按钮时在 Canvas 中初始化裁剪器,如下所示
$('#crop-mode').click(function(e){
var canvas=$('#image-display canvas')[0];
var options={
viewMode:0,
dragMode:'crop',
aspectRatio: NaN,
preview:'.extra-preview',
responsive:true,
cropBoxMovable: true,
cropBoxResizable: true,
autoCropArea:0.8
};
canvas.cropper(options);
canvas.on({
'build.cropper': function (e) {
console.log(e.type);
},
'built.cropper': function (e) {
console.log(e.type);
},
'cropstart.cropper': function (e) {
console.log(e.type, e.action);
},
'cropmove.cropper': function (e) {
console.log(e.type, e.action);
},
'cropend.cropper': function (e) {
console.log(e.type, e.action);
},
'crop.cropper': function (e) {
console.log(e.type, e.x, e.y, e.width, e.height, e.rotate, e.scaleX, e.scaleY);
},
'zoom.cropper': function (e) {
console.log(e.type, e.ratio);
}
});
});
我完全白费了
还有一个错误:canvas.on is not a function
我不知道为什么。我已经包含了 jQuery 3.1
最佳答案
来自 jQuery 文档:
Attach an event handler function for one or more events to the selected elements.
现在, var canvas=$('#image-display canvas')[0];
之后的 canvas
不是被选中的元素。它是所选元素的第一个属性。 $()
将返回一个选定的元素(如果找到,显然),但 $()[0]
则完全不同。放下 [0]
。
关于javascript - cropper 不是函数 on 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40005720/