效果

核心代码
<script>
//获取输入框 和 提示框
var con = document.querySelector('.con');
var input = document.querySelector('.jd');
//添加事件
input.addEventListener('keyup', function () { //当按键抬起时
//如果输入框内容不为空,则显示提示框
if (this.value != '') {
con.style.display = 'block';
//将this 当前输入的值赋给提示框
con.innerText = this.value;
}
//如果输入框内容为空,则不显示提示框
else {
con.style.display = 'none';
}
})
//失去焦点时,不显示con盒子
input.addEventListener('blur', function () {
con.style.display = 'none';
})
//获得焦点时,显示con盒子
input.addEventListener('focus', function () {
con.style.display = 'block';
})
</script>
全部代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
body {
margin: 0;
padding: 0;
}
.search {
position: relative;
width: 170px;
margin: 100px;
}
.search input {
outline: none;
}
.con {
/* 一开始的display默认隐藏 */
display: none;
position: absolute;
top: -40px;
width: 171px;
border: 1px solid rgba(0, 0, 0, .2);
box-shadow: 0 2px 4px rgba(0, 0, 0, .2);
padding: 5px 0;
/* 字体要比输入框大号 */
font-size: 18px;
line-height: 20px;
color: #333;
}
</style>
</head>
<body>
<div class="search">
<div class="con">123</div>
<input type="text" placeholder="请输入快递单号" class='jd'>
</div>
<script>
//获取输入框 和 提示框
var con = document.querySelector('.con');
var input = document.querySelector('.jd');
//添加事件
input.addEventListener('keyup', function () { //当按键抬起时
//如果输入框内容不为空,则显示提示框
if (this.value != '') {
con.style.display = 'block';
//将this 当前输入的值赋给提示框
con.innerText = this.value;
}
//如果输入框内容为空,则不显示提示框
else {
con.style.display = 'none';
}
})
//失去焦点时,不显示con盒子
input.addEventListener('blur', function () {
con.style.display = 'none';
})
//获得焦点时,显示con盒子
input.addEventListener('focus', function () {
con.style.display = 'block';
})
</script>
</body>
</html>