浅谈JavaScript实现文本框的placeholder
来源:互联网 发布:分级诊疗 知乎 编辑:程序博客网 时间:2024/06/06 01:12
代码:
<!doctype html> <html> <head> </head> <body> <input id="input" type="text" value="请输入关键词"> </body> <script>window.onload = function() {var defaultValue = "请输入关键词";var input = document.getElementById("input");input.style.color = "grey";input.onfocus = function() {if (this.value == defaultValue) {setCursorPosition(this, 0);}};input.onkeypress = function(e) {e = e || window.event;var key = e.charCode || e.keyCode || e.which;if (this.value == defaultValue) {this.value = "";this.style.color = "black";}if (this.value.length == 1 && key == 8) {this.value = defaultValue;this.style.color = "grey";setCursorPosition(this, 0);}};};function setCursorPosition(elem, index) { if (elem.setSelectionRange) {elem.focus();elem.setSelectionRange(index, index);}else if (elem.createTextRange) {var range = elem.createTextRange();range.collapse(true);range.moveEnd('character', index);range.moveStart('character', index);range.select();}} </script></html>
1 0
- 浅谈JavaScript实现文本框的placeholder
- 用原生Javascript实现css的placeholder
- JavaScript:掩码文本框的实现
- JavaScript 实现文本框的默认值
- javascript 基础 js实现placeholder
- 用javascript实现服务器端文本框的验证
- 用JavaScript实现的文本框光标定位
- IE下实现placeholder效果,支持文本框和密码框
- 浅谈Spring的 context:property-placeholder
- 文本框默认提示 placeholder
- 文本框 placeholder 兼容IE7 IE 8不能显示的效果
- JavaScript文本框的操作
- JavaScript文本框的操作
- JavaScript文本框的操作
- javascript实现的一个带下拉框功能的文本框
- JavaScript实现几个文本框同步
- UITextView 实现placeholder的方法
- UITextView 实现placeholder的方法
- 在构造函数中开辟内存空间
- 展望——量化未来真的能跑赢曾经的历史么?(一)
- Ubuntu14.04 下更新cmake2.8.x至3.2.2
- twitter 雪花算法 golang 实现
- SuperAgent
- 浅谈JavaScript实现文本框的placeholder
- direction和unicode-bidi
- 在centos 7中搭建lamp平台
- stringstream的用法
- MyISAM 和InnoDB
- (GIS可视化)热点分析原理及实现
- (转-原作者不详)-浅析JavaScript访问对象属性和方法及区别
- poj 2299 树状数组(离散化)求逆序数
- 数据流重定向-导出训练日志