自定义控件之 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可设置水印,可设置必填和正则表达式验证. 验证?没错,就是验证! 就是在输入完成后,控件一旦失去焦点就会自动验证!会根据我开放出来的“是否可以为空”属性进 ...
随机推荐
- .NET MVC3中扩展一个HtmlHelper方法CheckBoxList
MVC中有DropDownList方法,挺好用,可是最常用的需求,一组checkboxlist咋没个类似方法呢?郁闷之余,自己做一个吧,直接上代码 public static MvcHtmlStrin ...
- Qt之重写QLabel类
在mylabel.h 文件中#ifndef MYLABEL_H#define MYLABEL_H #include <QLabel>/*重新实现QLabel类,使其支持点击事件*/clas ...
- android sdk manager 无法更新解决方法
因为在开始->运行->cmd 中敲入 ping dl-ssl.google.com -t 始终ping不通 ,关闭cmd后 首先需要下载一个代理服务器下载地址 http://pan.bai ...
- linux bash快捷键
bash快捷键 CTRL+F 光标向前移动一个字母 CTRL+B 光标向后移动一个字母 CTRL+A HOME CTRL+E END
- jQuery 选择器 (基础恶补之二)
返回 CSS 属性 如需返回指定的 CSS 属性的值,请使用如下语法: css("propertyname"); 下面的例子将返回首个匹配元素的 background-color ...
- R----plotly包介绍学习
plotly包:让ggplot2的静态图片变得可交互 Plotly 是个交互式可视化的第三方库,官网提供了Python,R,Matlab,JavaScript,Excel的接口,因此我们可以很方便地在 ...
- Codeforces Round #356 (Div. 2)
A. Bear and Five Cards time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- JMeter结果分析
- createjs 的 bitmapdata类
今天测试一个功能,在效率上出现了问题.2D舞台绘制了大量的元素,联想到AS3的 bitmapdata.darw() 功能,遗憾是createjs官方类 中没有bitmapdata类. 好在已经有大神替 ...
- Windows 域(domain)
http://baike.baidu.com/view/1512519.htm http://baike.baidu.com/view/1218493.htm http://www.jb51.net/ ...