我該怎么做,當我的游標離開輸入欄位時,會觸發一個更改輸入背景顏色的函式。我試圖這樣做,但效果不佳。
<!DOCTYPE html>
<html>
<body>
Enter your name: <input type="text" id="name" onchange="changeColor()">
<script>
function changeColor() {
const x = document.getElementById("name");
x.style.background = "red";
}
</script>
</body>
</html>
uj5u.com熱心網友回復:
function changeColor(ev) {
ev.target.style.backgroundColor = "red";
}
uj5u.com熱心網友回復:
您可以使用該blur事件來專門針對失去焦點:
Enter your name: <input type="text" id="name">
<script>
const x = document.getElementById("name");
x.onblur = changeColor;
function changeColor() {
x.style.background = "red";
}
</script>
uj5u.com熱心網友回復:
嘗試 onm ouseleave 事件監聽器。onchange 通常涉及更改輸入欄位的值。
在 CSS 中,你有 :focus 選擇器來根據輸入是否聚焦來設定樣式
uj5u.com熱心網友回復:
它接縫你正在尋找onfocusout事件。
function changeColor(ev) {
if (ev.value.length > 0) {
ev.style.background = "red";
}
}
Enter your name: <input type="text" id="name" onfocusout="changeColor(this)">
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/455675.html
標籤:javascript html
