我正在使用一本书来学习 Javascript 教程。
练习是更改 <input type="search">
中的背景颜色使用 document.querySelector
.当我尝试在搜索框中搜索没有文本的内容时,<input>
的背景变为红色。我是用 onsubmit
做的和一些有条件的。但在下一部分中,它必须使用 onfocus
返回白色背景。我没有得到。
我试过的代码是
document.querySelector('#form-busca').onsubmit = function() {
if (document.querySelector('#q').value == '') {
document.querySelector('#q').style.background = 'red';
return false;
}
}
document.querySelector('#form-busca').onfocus = function() {
document.querySelector('#q').style.background = 'white';
}
有人能帮我吗?非常感谢!
最佳答案
几乎明白了,伙计。
改变:
document.querySelector('#form-busca').onfocus
至:
document.querySelector('#q').onfocus
修改后的代码:
正确样本:
document.querySelector('#form-busca').onsubmit = function() {
if (document.querySelector('#q').value == '') {
document.querySelector('#q').style.background = 'red';
return false;
}
}
document.querySelector('#q').onfocus = function() {
document.querySelector('#q').style.background = 'white';
}
关于javascript - 使用 querySelector 更改颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38003099/