<input type="text">
<script>
const input = document.querySelector('input')
input.addEventListener('focus', function () {
console.log('focus')
})
input.addEventListener('blur', function () {
console.log('blur')
})
</script>
触发下拉菜单的示例代码:
<script>
// 属性选择器
const input = document.querySelector('[type=search]')
const ul = document.querySelector('.result-list')
// console.log(input);
input.addEventListener('focus', function () {
ul.style.display = 'block'
input.classList.add('search')
})
input.addEventListener('blur', function () {
ul.style.display = 'none'
input.classList.remove('search')
})
</script>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
ul {
list-style: none;
}
.mi {
position: relative;
width: 223px;
margin: 100px auto;
}
.mi input {
width: 223px;
height: 48px;
padding: 0 10px;
font-size: 14px;
line-height: 48px;
border: 1px solid #e0e0e0;
outline: none;
}
.mi .search {
border: 1px solid #ff6700;
}
.result-list {
display: none;
position: absolute;
left: 0;
top: 48px;
width: 223px;
border: 1px solid #ff6700;
border-top: 0;
background: #fff;
}
.result-list a {
display: block;
padding: 6px 15px;
font-size: 12px;
color: #424242;
text-decoration: none;
}
.result-list a:hover {
background-color: #eee;
}
</style>
<input type="text">
<script>
const input = document.querySelector('input')
input.addEventListener('keydown', function () {
console.log('down')
})
input.addEventListener('keyup', function () {
console.log('up')
})
input.addEventListener('input', function () {
console.log(input.value)
})
</script>