自定义控件之 TextBox
//textbox type
var boxType = {
WaterMarkBox: 0,
ValidateBox: 1,
SearchBox: 2
}
var textBoxObj = function(vid){
this.id = vid; //textbox's id
this.validateString = ""; //validate string
this.waterMarkString = "please fill the content"; //watermark string
this.width = 300; //textbox width
this.height = 22; //textbox height
this.type = boxType.WaterMarkBox; //textbox type
this.imgUrl = "graphics/search.png";
}
//set textbox width
textBoxObj.prototype.setWidth = function (wid) { this.width = wid };
//set textbox height
textBoxObj.prototype.setHeight = function (hei) { this.height = hei };
//set textbox type
textBoxObj.prototype.setType = function (tp) { this.type = tp };
//initial textbox
textBoxObj.prototype.initBox = function () {
var context = this;
if (context.type == boxType.WaterMarkBox) {
var $textbox = $("<input id='" + context.id + "_ipt' type='text' style='width:" + context.width + "px; height:" + context.height + "px;border: 1px solid #95B8E7;color:#cacacd;'/>");
$("#" + context.id).append($textbox);
$textbox.val(context.waterMarkString);
$textbox.on("focus", function () {
if ($textbox.val() == context.waterMarkString) {
$textbox.val("");
}
$textbox.attr("style", "width:" + context.width + "px; height:" + context.height + "px;border: 1px solid #C0FF00;");
});
$textbox.on("focusout", function () {
if ($textbox.val() == "") {
$textbox.val(context.waterMarkString);
$textbox.attr("style", "width:" + context.width + "px; height:" + context.height + "px;border: 1px solid #95B8E7;color:#cacacd;");
}
else
$textbox.attr("style", "width:" + context.width + "px; height:" + context.height + "px;border: 1px solid #95B8E7;");
});
}
else if (context.type == boxType.ValidateBox) {
var $textbox_validate = $("<input id='" + context.id + "_ipt' type='text' style='width:" + context.width + "px; height:" + context.height + "px;border: 1px solid #95B8E7;'/>");
$("#" + context.id).append($textbox_validate);
$textbox_validate.on("change",function () {
var string = $textbox_validate.val().trim();
if (string == "") {
$textbox_validate.attr("style", "width:" + context.width + "px; height:" + context.height + "px;border: 1px solid #95B8E7;")
}
else if (string.match(context.validateString) == null) {
$textbox_validate.attr("style", "width:" + context.width + "px; height:" + context.height + "px;border: 1px solid #95B8E7;background-color: #FC6B6B;background-image: url(graphics/fault.png);background-repeat: no-repeat;background-position: right center;");
}
else {
$textbox_validate.attr("style", "width:" + context.width + "px; height:" + context.height + "px;border: 1px solid #95B8E7;background-image: url(graphics/pass.png);background-repeat: no-repeat;background-position: right center;");
}
});
}
else if (context.type == boxType.SearchBox) {
$textbox_search = $("<input type='text' style=' width:" + context.width + "px; height:22px;border: 1px solid #95B8E7; background-image: url("+context.imgUrl+");background-repeat: no-repeat;background-position: right center;' />");
$("#" + context.id).append($textbox_search);
$textbox_search.on("keydown", function (key) {
if (key.keyCode == "13") {
context.onSearch($textbox_search.val());
}
});
}
}
//press enter and search event
textBoxObj.prototype.onSearch = function (content) {
alert(content);
}
//set validate rule
textBoxObj.prototype.setValidateString = function (rule) {
this.validateString = rule;
}
//set watermark message
textBoxObj.prototype.setWaterMarkString = function (watemark) {
this.waterMarkString = watemark;
}
//for test
//$(function () {
// var o = new textBoxObj('cc2');
// o.setType(boxType.ValidateBox);
// o.initBox();
// o.setValidateString("^[0-9]*$");
//});
自定义控件之 TextBox的更多相关文章
- Web控件
Web控件可分三类 HTML控件 html服务器控件是在HTML控件的基础上,额外增加了一个在当前页面唯一的ID属性值和一个runat = "server" 属性html服务器控件 ...
- WPF自定义控件与样式(3)-TextBox & RichTextBox & PasswordBox样式、水印、Label标签、功能扩展
一.前言.预览 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等,若有不明白的地方可以参考本系列前面的文章,文末附有部分文章链接. 本文主要是对文本 ...
- WinForm程序中的类TextBox的自定义控件, 添加失去焦点的功能
原理: 一.在控件的后台代码中, 添加布尔类型的属性CanFocus 二.在控件的构造函数中, 注册Enter事件的处理方法. 并在处理方法中,根据CanFocus属性的值来决定是否可以丢失焦点, 如 ...
- TextBox自定义控件
首先来一发图: 今天主要说的textBox内部给予提示: 使用自定义控件方式:TextBoxTip继承TextBox 利用TextBox的背景画刷功能 VisualBrush是一种比较特殊的笔刷,它的 ...
- 【转】WPF自定义控件与样式(3)-TextBox & RichTextBox & PasswordBox样式、水印、Label标签、功能扩展
一.前言.预览 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等. 本文主要是对文本输入控件进行样式开发,及相关扩展功能开发,主要内容包括: 基本文 ...
- WinForm自定义控件–TextBox扩展
一.简单回顾 在前两节中,对Panel和GroupBox控件进行了相关的扩展应用,主要都是设置控件的边框以及边框颜色等.本节,继续对WinForm现有的控件TextBox进行扩展,来满足实际开发中 ...
- 工作记录--WPF自定义控件,实现一个可设置编辑模式的TextBox
原文:工作记录--WPF自定义控件,实现一个可设置编辑模式的TextBox 1. 背景 因为最近在使用wpf开发桌面端应用,在查看页面需要把TextBox和Combox等控件设置为只读的.原本是个很简 ...
- (八十九)c#Winform自定义控件-自定义滚动条(treeview、panel、datagridview、listbox、listview、textbox)
官网 http://www.hzhcontrols.com/ 前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kw ...
- WPF自定义控件(二)——TextBox
和之前一样,先来看看效果: 这个TextBox可设置水印,可设置必填和正则表达式验证. 验证?没错,就是验证! 就是在输入完成后,控件一旦失去焦点就会自动验证!会根据我开放出来的“是否可以为空”属性进 ...
随机推荐
- C++之路进阶——bzoj2879(美食节)
2879: [Noi2012]美食节 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 1304 Solved: 702[Submit][Status] ...
- 上传图片预览设置src不显示
使用滤镜效果解决: window.parent.document.body.focus(); var imgurl = (document.selection.createRange().text). ...
- js与jsp
jsp :j2ee 中的一样模版技术,运行于服务器端javascript :一种运行于客户端的脚本语言,动态性.JavaScript是一种采用事件驱动的脚本语言,它不需要经过Web服务器就可以对用户的 ...
- lipo 合并target为Simulator和Device编译的静态库
进入项目对应的Build目录后,以下指令: $lipo -create Debug-iphoneos/libSalamaDeveloper.a Debug-iphonesimulator/libSal ...
- selenium杀掉浏览器进程方法
* 杀掉浏览器进程 */ public static void operateWindowsProcess(){ WindowsUtils.tryToKillByNa ...
- MySQL 常用命令
mysql -uroot -pXXXXXXXXX //登陆mysql mysqladmin -u 用户名 -p 旧密码 password 新密码 创建数据库并分配用户 create database ...
- C# 蓝牙编程
C#进行蓝牙编程 本节我们给大家用源码的形式给大家介绍如何用C#调用蓝牙.下面的源码是基于destop的C#调用蓝牙的程序,也就是使用普通版本的.NET Framework来调用编程,一般是有蓝牙的笔 ...
- Java-String类的常用方法总结
一.String类String类在java.lang包中,java使用String类创建一个字符串变量,字符串变量属于对象.java把String类声明的final类,不能有类.String类对象创建 ...
- Linux 休眠,挂起(待机),关机等几个命令的区别及如何实现;如何启用Ubuntu的休眠模式
这里对linux 的几个命令整理下,有:休眠,挂起,待机,关机等几个命令的区别及如何实现. 休眠是一种更加省电的模式,它将内存中的数据保存于硬盘中,所有设备都停止工作.当再次使用时需按开关机键,机器将 ...
- C#连接上sql server 2008 第一次实践
花了一早上的时间,终于连接上了我的本地数据库,我想应该记一下! 先贴个代码: using System; using System.Collections.Generic; using System. ...