Javascript 使用querySelector更改颜色
我正在使用一本书学习Javascript教程。 练习是使用Javascript 使用querySelector更改颜色,javascript,onfocus,Javascript,Onfocus,我正在使用一本书学习Javascript教程。 练习是使用文档.querySelector更改中的背景色。当我试图搜索搜索框中没有文本的内容时,中的背景变为红色。我使用了onsubmit和一些条件。但在下一部分中,它必须使用onfocus返回到白色bckground,而我不明白 我试过的代码是 document.querySelector('#form-busca').onsubmit = function() { if (document.querySelector('#q').val
文档.querySelector
更改
中的背景色。当我试图搜索搜索框中没有文本的内容时,
中的背景变为红色。我使用了onsubmit
和一些条件。但在下一部分中,它必须使用onfocus
返回到白色bckground,而我不明白
我试过的代码是
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';
}
有人能帮我吗?非常感谢 当
input
元素被聚焦时,听起来您希望输入的背景色变为白色。
尝试将
onfocus
选择器更改为:
document.querySelector(“#q”).onfocus
..您希望onfocus
处理程序位于#q
元素上,而不是位于#form busca
上;表单的焦点无关紧要,您希望在输入元素获得焦点时清除背景:
document.querySelector('#q').onfocus = function() { ... }
差不多了,伙计
更改:
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';
}
请尝试以下代码:
// select
var h1 = document.querySelector("h1");
// manipulate
h1.style.color = "red";
这将使
h1
的颜色变为红色。明白了!谢谢,老兄:我正试着投票,但那里有一些错误D:当我投票的时候,它会回到零lol