您的位置:首页 > 其它

仿淘宝搜索框之---ie9以下版本placeholder效果

2017-11-07 11:56 686 查看
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>仿淘宝搜索框</title>
<style>
.search{
width: 300px;
height: 30px;
margin: 100px auto;
position: relative;
}
.search input {
width: 200px;
height: 25px;
}
.search label{
font-size: 12px;
color: #CCCCCC;
position: absolute;
top: 8px;
left: 10px;
cursor: text;
}
</style>
<script type="text/javascript">
window.onload = function(){
function $(id){
return document.getElementById(id);
}
$('txt').focus();
$('txt').oninput = $('txt').onpropertychange = function(){
if(this.value == ""){
$('message').style.display = 'block';
} else {
$('message').style.display = 'none';
}
}
}
</script>
</head>
<body>
<div class="search">
<input type="text" id="txt" />
<label for="txt" id="message">puma女鞋</label>
</div>
</body>
</html>


打开页面,搜索框中就可以看到灰色字体“puma女鞋”并自动获取光标。点击输入,灰色字消失。清空文本框的所有内容,灰色字自动恢复。

判断用户输入的事件有 oninput 和onpropertychange 。由于浏览器兼容的问题,他们出现的场合有所不同。 正常浏览器支持oninput ,而 IE6、IE7、IE8 支持的 onpropertychange 。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: