2010-10-10 80 views
3

今天我來演示一下jquery中opera的錯誤,關於對象轉換,下面是代碼(函數setColor(x,y)):Opera error:未捕獲的異常:TypeError:無法將'xxxxxx'轉換爲對象

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]+')'; 

這裏是歌劇拋出的錯誤:

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 =新的Array();)的對象,但沒有運行。

預先感謝您!

+0

@Nick「歌劇引發的錯誤」 – lonesomeday 2010-10-10 10:57:39

+0

@Angelus爲什麼不把最後一個括號添加到正則表達式中並執行'canvascontext.fillStyle = colourpixel [0];'? – lonesomeday 2010-10-10 11:01:55

+0

因爲如果我把一個;那裏,有一個sintax問題寬度字符串連接沒有? – 2010-10-10 11:03:17

回答

1

我不知道最好的修復,因爲我沒有這個顏色處理情況在所有的,但這裏的問題是:

在歌劇,當你設定一個風格rgb(...),例如:

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

在大多數瀏覽器是爲$('#colour').css('background-color')你會得到:"rgb(0, 70, 255)",但事實並非如此在Opera中,你會得到一個"#0046ff"十六進制格式,讓你的正則表達式將不匹配,colourpixelnull,而不是匹配數組。這會導致您的錯誤,與null[1]一樣。

Here's a quick test to demonstrate this,在任何其他主要瀏覽器中測試它,然後Opera。

相關問題