自定义控件之 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开发实战1-军队未出,粮草先行。
			马上期末考试了,会想起这个学习的自己.一直都在一个人搞java的研究,C#课也没怎么去上.所以在这里想弥补一下自己没去上课的原因,也希望老师能够理解这个还在迷茫的我. 正所谓,军队未出粮草先行,所以我 ... 
- pb数据窗口设置操作
			1 使DataWindow列只能追加不能修改如何使DataWindow中的数据只能追加新记录而不能修改,利用 Column 的 Protect 属性可以很方便的做到这一点,方法如下:将每一列的 Pro ... 
- SQL中 Left Join 与 Right Join 与 Inner Join 与 Full Join的区别
			首先看看Left Join 与Right Join 与 Inner Join 与 Full Join对表进行操作后得到的结果. 在数据库中新建两张表,并插入要测试的数据. 新建表: GO /***** ... 
- 13. 星际争霸之php设计模式--正面模式
			题记==============================================================================本php设计模式专辑来源于博客(jymo ... 
- [转]网络诊断工具:MTR
			MTR是Linux平台上一款非常好用的网络诊断工具,集成了traceroute.ping.nslookup的功能,用于诊断网络状态非常有用.能按要求对路由中所有节点进行批量测试 第一列(Host):I ... 
- 复旦大学2014--2015学年第二学期高等代数II期末考试情况分析
			一.期末考试成绩班级前几名 钱列(100).王华(92).李笑尘(92).金羽佳(91).李卓凡(91).包振航(91).董麒麟(90).张钧瑞(90).陆毕晨(90).刘杰(90).黄成晗(90). ... 
- Winform开发框架之单据窗体生成(主从表,流水单号)
			源码地址:https://github.com/GarsonZhang/GZFramework.ShareDemo 前言 1.在开始本节前请先重置代码为 chapter-03-start 懒人地址:h ... 
- 关于JS中apply方法的基本理解
			最近研究OpenLayers源码时,发现其中使用了比较多的apply方法,对其也是很不明白.于是上网经过多方面了解以及自己细细体会后,终于算是基本明白是其干什么的了,这里分享下.apply方法的造型是 ... 
- Android NDK开发入门实例
			AndroidNDK是能使Android应用开发者把从c/c++编译而来的本地代码嵌入到应用包中的一系列工具的组合. 注意: AndroidNDK只能用于Android1.5及以上版本中. I. An ... 
- javaSE之Object及hashcode等相关知识
			object: package javaBasic; public class TestObject { public static void main(String[] args) { // TOD ... 
