Opera 错误:未捕获异常:TypeError:无法转换“xxxxxx”反对
我今天来展示Jquery中opera抛出的错误,关于对象转换,这里是代码(函数setColor(x,y)):
colourpixel = $('#colour').css('background-color').match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);//["rgb(0, 70, 255", "0", "70", "255"]
var canvas = document.createElement('canvas');
canvas.height=190;
canvas.width=190;
canvascontext = canvas.getContext("2d");
defaultdata = $('#light').get(0).getContext("2d").getImageData(0,0,190,190);
canvascontext.putImageData(defaultdata,0,0);
canvascontext.globalCompositeOperation = 'destination-atop';
canvascontext.fillStyle='rgb( '+colourpixel[1]+', '+colourpixel[2]+', '+colourpixel[3]+')';
这是opera抛出的错误:
Uncaught exception: TypeError: Cannot convert 'colourpixel' to object
Error thrown at line 157, column 1 in setColor(x, y) in file://localhost/home/angelus/Desarrollo/webs/ColorP/functions.js:
canvascontext.fillStyle='rgb( '+colourpixel[1]+', '+colourpixel[2]+', '+colourpixel[3]+')';
called from line 61, column 2 in <anonymous function>(event) in file://localhost/home/angelus/Desarrollo/webs/ColorP/functions.js:
setColor(x,y);
called from line 55, column 294 in <anonymous function: handle>(a) in file://localhost/home/angelus/Desarrollo/webs/ColorP/jquery.min.js:
i=i.handler.apply(this,arguments);
called via Function.prototype.apply() from line 49, column 569 in <anonymous function: o>() in file://localhost/home/angelus/Desarrollo/webs/ColorP/jquery.min.js:
return typeof c!=="undefined"&&!c.event.triggered?c.event.handle.apply(o.elem,arguments):w
我尝试像数组一样创建对象( var colorpixel = new Array(); ) 但没有运行。
先感谢您!
i come today to show an error thrown by opera in Jquery ,about object transformation , here is the code ( function setColor(x,y) ):
colourpixel = $('#colour').css('background-color').match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);//["rgb(0, 70, 255", "0", "70", "255"]
var canvas = document.createElement('canvas');
canvas.height=190;
canvas.width=190;
canvascontext = canvas.getContext("2d");
defaultdata = $('#light').get(0).getContext("2d").getImageData(0,0,190,190);
canvascontext.putImageData(defaultdata,0,0);
canvascontext.globalCompositeOperation = 'destination-atop';
canvascontext.fillStyle='rgb( '+colourpixel[1]+', '+colourpixel[2]+', '+colourpixel[3]+')';
And here is the error thrown by opera :
Uncaught exception: TypeError: Cannot convert 'colourpixel' to object
Error thrown at line 157, column 1 in setColor(x, y) in file://localhost/home/angelus/Desarrollo/webs/ColorP/functions.js:
canvascontext.fillStyle='rgb( '+colourpixel[1]+', '+colourpixel[2]+', '+colourpixel[3]+')';
called from line 61, column 2 in <anonymous function>(event) in file://localhost/home/angelus/Desarrollo/webs/ColorP/functions.js:
setColor(x,y);
called from line 55, column 294 in <anonymous function: handle>(a) in file://localhost/home/angelus/Desarrollo/webs/ColorP/jquery.min.js:
i=i.handler.apply(this,arguments);
called via Function.prototype.apply() from line 49, column 569 in <anonymous function: o>() in file://localhost/home/angelus/Desarrollo/webs/ColorP/jquery.min.js:
return typeof c!=="undefined"&&!c.event.triggered?c.event.handle.apply(o.elem,arguments):w
I have tried to create the object like an array ( var colourpixel = new Array(); ) but nothing run.
Thank you in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定最好的解决方案,因为我根本不处理这种颜色情况,但问题是:
在 Opera 中,当您将样式设置为
rgb(...)
时,例如:在大多数浏览器中
$('#colour').css('background-color')
您将得到:"rgb(0, 70, 255)",但在 Opera 中并非如此,您将得到
"#0046ff"
的十六进制格式,因此您的正则表达式将不匹配,并且colourpixel
将是null
,不是匹配数组。这会导致您的错误,与null[1]
相同。这里有一个快速测试来演示这一点,在任何其他主要浏览器中进行测试,然后在 Opera 中进行测试。
I'm not sure the best fix since I don't deal with this color situation at all, but here's that the problem is:
In opera when you set a style as
rgb(...)
, for example:In most browser's for
$('#colour').css('background-color')
you'll get:"rgb(0, 70, 255)"
, but that's not true in Opera, you'll get a hex format of"#0046ff"
, so your regex won't match andcolourpixel
will benull
, not an array of matches. This causes your errors, the same asnull[1]
would.Here's a quick test to demonstrate this, test it in any other major browser, then Opera.