Opera 错误:未捕获异常:TypeError:无法转换“xxxxxx”反对

发布于 2024-09-27 00:03:57 字数 1652 浏览 3 评论 0原文

我今天来展示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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柏拉图鍀咏恒 2024-10-04 00:03:57

我不确定最好的解决方案,因为我根本不处理这种颜色情况,但问题是:

在 Opera 中,当您将样式设置为 rgb(...) 时,例如:

<div id="colour" style="background-color: rgb(0, 70, 255);">​

在大多数浏览器中 $('#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:

<div id="colour" style="background-color: rgb(0, 70, 255);">​

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 and colourpixel will be null, not an array of matches. This causes your errors, the same as null[1] would.

Here's a quick test to demonstrate this, test it in any other major browser, then Opera.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文