JS/HTML:将类添加到搜索输入的父级不起作用
JS / HTML : Adding class to parent of search input not working
嗨,我正在尝试选择搜索输入的父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
还可以尝试使用小提琴顶部的"JSHint"按钮。
好的IDE也会为您突出这些错误。使用适当的缩进还有助于找到未关闭的处理程序:
$(function () {
$('.box').focus(function () {
$(this).parent().addClass('fill');
});
});
上面的格式是使用jsfiddle的"整理"按钮获得的。这是你最新的小提琴。
jsFiddle上的工作示例。
$('.box').on('blur', function () {
$(this).parent()
.addClass('fill');
});
您的JavaScript:中存在错误
更改此项:
$('.box').focus(function(){
$(this).parent().addClass('fill');
)};
至:
$('.box').focus(function(){
$(this).parent().addClass('fill');
});
(注意翻转的大括号顺序)
你的JS中有一个拼写错误,应该是这样的
$(function(){
$('.box').focus(function(){
$(this).parent().addClass('fill');
});
});
看起来有语法错误。这是正确的代码:
$(function(){
$('.box').focus(function(){
$(this).parent().addClass('fill');
});
});
试试这个。
$(function(){
$('.box').focus(function(){
$(this).parent().addClass('fill');
});
});
相关文章:
- 使用javascript搜索具有用户输入的数组
- Javascript:使用用户输入搜索数组
- jQuery用fiddle在标题中查询移动搜索输入(标题的覆盖)
- 如何在不使用任何 html 输入/搜索元素的情况下读取本地客户端文本文件
- 如何在不隐藏键盘的情况下清除输入搜索?(手机)
- 如何以编程方式输入搜索字符串并触发谷歌地图API的places_changed
- [ jQuery/Datatable ]:数据表无响应,禁用输入搜索
- 在多个网站上输入搜索并截屏的Javascript
- 仅在 Firefox 中,当我单击任何地方时,我的 Twitter 提前输入搜索输入会清除
- (Phonegap应用)如何在jQuery Mobile中传递两个页面的输入搜索值
- 将光标放到输入搜索字段的末尾
- 我如何将数据放入下拉框中,并能够通过输入搜索它
- 如何在jquery中改变输入搜索数据表的方向
- 奇怪的点击事件模式,输入搜索框和无序列表
- 如何使向下箭头在输入搜索框上工作
- javascript来隐藏所有与文本输入搜索框的内容不对应的客户端
- 使用用户输入搜索 JSON 解析的 MySQL 数据
- 输入搜索字段在响应式网站的移动版本上正常工作
- 如何实现下拉列表值的输入搜索
- 如何添加“输入搜索”选择