嘗試將this.value作爲參數傳遞時,我不斷收到意外的代碼}錯誤。Javascript:意外的代幣}
在我的身上,我有:
<input type="text" size="3" id="rbgTextbox" onkeypress="if (ValidateRGBTextboxInput(event)) ChangeBackgroundColor("+this.value+"); else return false;">
功能:
<script type="text/javascript">
function ValidateRGBTextboxInput(e) {
return ById('rbgTextbox').value.length < 6 &&
(e.which >= 48 &&
e.which <= 57 ||
e.which >= 65 &&
e.which <= 70 ||
e.which >= 97 &&
e.which <= 102);
}
function ChangeBackgroundColor(color) {
alert(color);
ById('gameArea').style.backgroundColor = '#'+color;
ById('nextPiece').style.backgroundColor = '#'+color;
}
</script>
如果我改變ChangeBackgroundColor( 「+ THIS.VALUE +」);爲ChangeBackgroundColor('000000');我沒有得到更多的錯誤,但我當然需要傳遞文本輸入的值作爲參數。
除了在IE瀏覽器(以及像IE瀏覽器那樣將瀏覽器特定代碼投入骨骼之類的瀏覽器)之外,'event'的使用將無法使用。此外,您調用'CheckKeyPress'的方式,'this'不會是元素引用(它將成爲全局對象)。 –