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

<!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 。





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值