Opera错误:未捕获异常:TypeError:无法将'xxxxxx'转换为对象

时间:2010-10-10 10:45:18

标签: javascript jquery css opera uncaught-exception

我今天来看看在Jquery中出现的一个错误,关于对象转换,这里是代码(函数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 colourpixel = new Array();)但没有运行。

提前谢谢!

1 个答案:

答案 0 :(得分:1)

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

在歌剧中将样式设置为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]相同。

Here's a quick test to demonstrate this,在任何其他主流浏览器中测试,然后是Opera。