自定义控件之 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可设置水印,可设置必填和正则表达式验证. 验证?没错,就是验证! 就是在输入完成后,控件一旦失去焦点就会自动验证!会根据我开放出来的“是否可以为空”属性进 ...
随机推荐
- POJ - 2183 Bovine Math Geniuses
“模拟“题,运用哈希,不断地按照一定运算规律对一个结果进行计算,如果重复出现就停止并且输出该数.注意到仔细看题,这种题一定要细心! POJ - 2183 Bovine Math Geniuses Ti ...
- CSS图片列表
1.效果图: 2.Example Source Code <h3><a href="http://www.52css.com/">我爱CSS画廊</a ...
- 06-BCD计数器设计与应用——小梅哥FPGA设计思想与验证方法视频教程配套文档
芯航线--普利斯队长精心奉献 实验目的:1.掌握BCD码的原理.分类以及优缺点 2.设计一个多位的8421码计数器并进行验证 3.学会基本的错误定位以及修改能力 ...
- Java基础 静态块、非静态块、构造函数的执行顺序
Java中经常有一些静态块,这是用来在生成类之前进行的初始化,无论java还C++语言中的static,都是最先初始化好的.结构如下: static { 静态语句代码块 } { 非静态语句代码块 } ...
- 用webdriver+phantomjs实现无浏览器的自动化过程
环境准备 1. 安装python: 2. 安装pip: 3. 通过pip安装selenium: 4. 下载phantomJS的包并解压缩: 1. 若在Windows系统中,将下载的phantomjs文 ...
- PetaPoco的几个特性
在PetaPoco中,Brad并没有定义太多Attribute来修饰Models或Fields.这些为数不多的几个Attribute如下: ColumnAttribute ExplicitColumn ...
- Intellij IDEA debug介绍
先编译好要调试的程序. 1.设置断点 选定要设置断点的代码行,在行号的区域后面单击鼠标左键即可. 2.开启调试会话 点击红色箭头指向的小虫子,开始进入调试. IDE下方出现Debug视图,红色的箭头指 ...
- TortoiseGit 连接每次都要输入用户名和密码
当你配置好git后,在C:\Documents and Settings\Administrator\ 或者 C:\Users\Administrator 目录下有一个 .gitconfig 的文件 ...
- 【RabbitMQ】RabbitMQ在Windows的安装和简单的使用
版本说明 使用当前版本:3.5.4 安装与启动 在官网上下载其Server二进制安装包,在Windows上的安装时简单的,与一般软件没什么区别. 安装前会提示你,还需要安装Erlang,并打开下载页面 ...
- C++高精度计算代码运行时间(转载)
转载:http://blog.csdn.net/rrrfff/article/details/6583410 //在定时前应该先调用QueryPerformanceFrequency()函数获得机器内 ...