2 回答

TA貢獻(xiàn)1898條經(jīng)驗(yàn) 獲得超8個(gè)贊
有一個(gè)非常優(yōu)雅的解決方案 - 不使用 javascript,而是使用 css 偽類焦點(diǎn):
:focus CSS 偽類表示已接收焦點(diǎn)的元素(例如表單輸入)。它通常在用戶單擊或點(diǎn)擊某個(gè)元素或使用鍵盤(pán)的 Tab 鍵選擇它時(shí)觸發(fā)。
在你的情況下:
.lime-focused-input:focus {
? ? background:lime;
}
<!DOCTYPE html>
<html>
<head>
? <meta charset="UTF-8">
? <meta name="viewport" content="width=device-width, initial-scale=">
? <title></title>
</head>
<body>
? <form action="">
? ? <label for="">Name</label> <input type="text" id="fname" class="lime-focused-input">
? ? <br><br>
? ? <label for="">class</label> <input type="text" class="lime-focused-input">
? ? <br><br>
? ? <label for="">Country</label>
? ? <select name="" id="">
? ? ? <option value="">Pakistan</option>
? ? ? <option value="">India</option>
? ? ? <option value="">America</option>
? ? ? <option value="">China</option>
? ? </select>
? </form>
</body>
</html>

TA貢獻(xiàn)1825條經(jīng)驗(yàn) 獲得超4個(gè)贊
我想如果您嘗試使用background透明更改 try 并且不要使用blur,它是一個(gè)沖突的關(guān)鍵字(原因見(jiàn)下文):
function abc(element) {
element.style.background = "lime";
}
function aaa(element) {
element.style.backgroundColor = "transparent";
}
<form action="">
<label for="">Name</label> <input type="text" id="fname" onfocus="abc(this)" onblur="aaa(this)">
<br><br>
<label for="">class</label> <input type="text" onfocus="abc(this)" onblur="aaa(this)">
<br><br>
<label for="">Country</label>
<select name="" id="">
<option value="">Pakistan</option>
<option value="">India</option>
<option value="">America</option>
<option value="">China</option>
</select>
</form>
默認(rèn)情況下,作用域是window作用域,window.blur()或者blur()是對(duì)象的事件處理程序window,并且不能被覆蓋。
因此您無(wú)法覆蓋任何這些window相關(guān)屬性。
添加回答
舉報(bào)