gpt4 book ai didi

javascript - JS/HTML : Adding class to parent of search input not working

转载 作者:太空宇宙 更新时间:2023-11-03 20:52:05 25 4
gpt4 key购买 nike

您好,我正在尝试选择搜索输入的父 DIV,以便在聚焦搜索输入时更改背景颜色。我很困惑为什么这段代码不起作用。感谢您的帮助。

http://jsfiddle.net/BZ5uf/1/

HTML

<div class="search">  
<input class="box" value="Search" onfocus="if(this.value=='Search') this.value='';" onblur="(this.value=='') this.value='Search';">
</div>

CSS

.search {
position: relative;
margin: 20px;
width: 300px;
height: 65px;
border: 1px solid black;

}
input.box {
margin: 10px;
}

.fill {
background: #222;
}

JavaScript

$(function(){
$('.box').focus(function(){
$(this).parent().addClass('fill');
)};

最佳答案

大量语法错误。

)}; //mismatched order

应该是

    });
}); //you also forgot to close DOM ready handler

也可以尝试使用 fiddle 顶部的“JSHint”按钮。

好的 IDE 也会为您突出显示这些错误。使用适当的缩进也有助于找到未关闭的处理程序:

$(function () {
$('.box').focus(function () {
$(this).parent().addClass('fill');
});
});

以上格式是使用jsfiddle的“TidyUp”按钮获得的。这是你的 updated fiddle .

关于javascript - JS/HTML : Adding class to parent of search input not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14782830/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com