在具有 JavaScript 的顏色型別輸入中,onchange 事件不生效
onchange 事件在元素改變時觸發。你需要使用以下語法。
onchange="yourFunctionName(this);"
示例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"> <script src="https://code.jquery.com/jquery-1.12.4.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> </head> <body> <p> <label for="color">Choose Color</label> <input type="color" id="selectedColor" value="#985d5d" onchange="showValue(this);" /> </p> <script> function showValue(c){ console.log(c.value); } </script> </body> </html>
要執行上述程式,請儲存檔名稱 **anyName.html(index.html)**,然後右鍵單擊該檔案,在 VS Code 編輯器中選擇開啟 live server 選項。
輸出
單擊顏色型別的輸入後,你將在控制檯中獲取顏色值 −
廣告