因为我不懂JQuery,所以我只会用纯JS写,我的目的是当input获得焦点后,为上面id为"inputGroup"的div添加一个类名focus,当失去焦点时,移除类名focus..在JS里我是这样写的最后失败了,求大神指点。
2 回答
已采纳
李晓健
TA贡献1036条经验 获得超461个赞
<!doctype>
<html>
<head>
<meta charset = "utf-8">
<title>xxxxx</title>
<style type="text/css">
.focus{
border: 1px solid red;
}
</style>
</head>
<body>
<div class="input-group" id="inputGroup">
<input type="search" onblur="inputOnBlur()" id="searchInput" onfocus="inputOnfocus()" class="form-control" placeholder="search">
</div>
<script>
var searchInput = document.getElementById('searchInput');
var inputGroup = document.getElementById('inputGroup');
function inputOnfocus(){
inputGroup.classList.add('focus');
}
function inputOnBlur(){
inputGroup.classList.remove('focus');
}
</script>
</body>
</html>请在chrome或IE10以上的浏览器上做测试
慕无忌5553096
TA贡献1条经验 获得超0个赞
$("#searchInput").focus(function(){
$("#searchInput").addClass("focus");
});添加回答
举报
0/150
提交
取消
