jquery打造自定义控件(原创)

news/2024/7/3 0:42:50 标签: javascript, xhtml

本人第一次发表文章,不足之出请大家多多包涵

 

 

 

 

下面是一个combox的代码  

 

javascript">/// <reference path="../Js/jquery-1.7.2.min.js" />

$.extend({

    //下拉列表框

    qyjcombox: function (select) {

        //这里是初始化

        var htmlstr = "<div class=\"boxwrap\"><a class=\"select-tit\" href=\"javascript:;\"><span></span><i></i></a><div class=\"select-items\" style=\"z-index: 1; display: none;\"><ul><li> ├ 房产动态</li></ul></div><i class=\"arrow\" style=\"z-index: 1; display: none;\"></i></div>";

        var cbjq = $(select);

        //在用户定义的节点中填充我们的combox

        cbjq.html(htmlstr);

        //并附上class

        cbjq.addClass("rule-single-select single-select");

        //要复制多次的节点 

        var copeItem = $("li", select);

        //要添加到的父节点

        var itemParant = copeItem.parent();

        //要复制的节点获取到父节点后马上删除

        copeItem.remove();

        //下拉选中后显示值的节点

        var showText=cbjq.find("span").first();

 

        //这里是列表框的业务逻辑

        

        //记录选项的数组

        var list = [];

        var isShow = true;

        //注册文本框单击事件

        $(".select-tit", select).click(HideOrShow);

 

        //显示或者隐藏

        function HideOrShow() {

                if (isShow) {

                    ShowList();

                } else {

                    hideList();

                }

        }

 

        // title 和id 后面的是自定义属性

        this.addItem = function (title, id, itemObj) {

            //复制节点

            var newItem = copeItem.clone();

            //给节点附名字

            newItem.html(title);

            newItem.bind("click", clickEvent);

            itemParant.append(newItem);

            list.push({

                title: title,

                id: id,

                itemObj: itemObj,

                dom: newItem

            });

 

        }

        //显示下拉列表

        function ShowList() {

            isShow = false;

            $(".select-items", select).css("display", "block");

            $(".arrow", select).css("display", "block");

        }

        //隐藏列表

        function hideList() {

            isShow = true;

            $(".select-items", select).css("display", "none");

            $(".arrow", select).css("display", "none");

        }

        function clickEvent() {

            var node = $(this);

            showText.html(node.html());

            hideList();

        }

 

        //注册选项改变事件

        this.itemChangeEvent = function (changFunc) {

            for (var i = 0; i < list.length; i++) {

                //利用函数变量的作用域和this关键字改变来用户调用方便

                (function (item) {

                    //现在这个item被这个匿名函数 独享了 随便你怎么玩

                    item.dom.bind("click",function (e) {

                        //触发外部的函数

                        changFunc.apply(this, [{ title: item.title, id: item.id, itemObj: item.itemObj }, e]);

                    });

                })(list[i]);

                //下面是我之前 ie9留下的坏习惯 ie8 只能上面这种写法

                //var item = list[i];

                //item.dom.bind("click",function(){changFunc.apply(this,{title:item.title,id:item.id}) });

            }

        }

 

 

    }

 

});

  

 

接下来就是 取css的环节了

 

 

 

 

 

 

下面是css代码

/* CSS Document */

*{ margin:0; padding:0; list-style:none;font-family: "Microsoft YaHei";}

table{ border-collapse:collapse; border-spacing:0; }

a{ color:#686f7f; text-decoration:none; }

a:link, a:visited{ color:#2A72C5; text-decoration:none; }

a:active, a:hover{ color:#0065D9; text-decoration:underline; }

.clearer{ clear:both;}

 

/* single-select */

.single-select{ position:relative; display:inline-block; margin-right:5px; vertical-align:middle; cursor:pointer; *float:left; }

.single-select .boxwrap{ display:inline-block; vertical-align:middle; }

.single-select .select-tit{ position:relative; display:block; padding:5px 38px 5px 10px; min-width:40px; line-height:20px; height:20px; border:solid 1px #eee; text-decoration:none; background:#fff; white-space:nowrap; word-break:break-all; }

.single-select .select-tit span{ display:inline-block; color:#333; font-size:12px; vertical-align:middle; }

.single-select .select-tit i{ position:absolute; right:0; top:0; display:block; width:28px; height:100%; border-left:1px solid #eee; background:url(images/skin_icons.png) 7px -189px no-repeat #fafafa; }

.single-select .select-items{ display:none; position:absolute; left:0; top:45px; /*overflow:hidden;*/ }

.single-select .select-items ul{ position:relative; padding:5px; min-width:120px; max-height:280px; border:1px solid #eee; background:#fff; overflow-y:auto; overflow-x:hidden; }

.single-select .select-items ul li{ display:block; padding:4px 10px; line-height:20px; font-size:12px; color:#666; white-space:nowrap; cursor:pointer; }

.single-select .select-items ul li:hover{ color:#fff; text-decoration:none; background:#16a0d3; }

.single-select .select-items ul li.selected{ color:#fff; background:#16a0d3; }

.single-select .arrow{ display:none; position:absolute; left:15px; top:35px; width:21px; height:11px; text-indent:-9999px; background:url(images/skin_icons.png) 0 -290px no-repeat; }

.single-select.up .select-items{ top:auto; bottom:45px; }

.single-select.up .arrow{ top:-13px; background:url(../images/skin_icons.png) 0 -300px no-repeat; }

 

 

后来发现里面有图片 

 

background:url(images/skin_icons.png) 改成当前路径就ok了 

 

 

 

 

完工就可以测试了

 

测试html

 

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

    <title></title>

    <link href="combox/combox.css" rel="stylesheet" />

    <script src="Js/jquery-1.7.2.min.js"></script>

    <script src="combox/combox.js"></script>

    <script>

        $(window).ready(function () {

            var com = new $.qyjcombox("#cm");

            com.addItem("测试1", 1);

            com.addItem("测试2", 2);

            com.addItem("测试3", 3);

            com.itemChangeEvent(function (data) {

                alert("你单击了:"+data.title+" id:"+data.id);

            });

        });

 

 

    </script>

</head>

<body>

    <div id="cm"></div>

</body>

</html>

  

 

效果图 环境 360浏览器 内核 ie7

 

 

控件下载地址 :http://pan.baidu.com/s/1c0cxQ28

工具下载地址:http://pan.baidu.com/s/1qWC7lRu

转载于:https://www.cnblogs.com/QDSBK/p/4604674.html


http://www.niftyadmin.cn/n/1153074.html

相关文章

LeetCode 9. Palindrome Number(c语言版)

题目&#xff1a; Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward. Example 1: Input: 121 Output: trueExample 2: Input: -121 Output: false Explanation: From left to right, it reads -121. Fro…

生产者--消费者模式

生产者--消费者模式 1、示例&#xff1a; class Resource{private String name;private int count 1;private Boolean flag false;public synchronized void set(String name){if(flag)try {this.wait();//this代表调用函数线程} catch (InterruptedException e) {}this.name…

C++中的预定义宏

C/C&#xff0b;&#xff0b;宏大全(转载,原贴地址http://www.cnblogs.com/sevencat/archive/2004/06/10/14872.html)一、标准预定义宏The standard predefined macros are specified by the relevant language standards, so they are available with all compilers that impl…

PHP 7.0 中各种 Hash 速度比较

概述 最近需要对一些很长的 msyql 字段做索引优化。讨论下来有几种解决方案带确定&#xff0c;其中一个就是对现有字符做 hash&#xff0c;然后对此hash和原始字符做联合索引。就此有了 hash 效率比较的需求&#xff0c;文中使用 php 对一段字符做 200 万次 hash&#xff0c;并…

NPOI兼容 excel2003,2007版本

根据项目需要&#xff0c;需要对excel进行导入导出&#xff0c;所以选择NPOI&#xff0c;优点在这里就不详细介绍了&#xff0c;下面进入正题。 1 public int Import(string path)2 {3 IList<Student> list new List<Student>();4 5 …

解决 Zend Studio For Linux 乱码和UBUNTU下不显示(白屏)问题

不显示界面问题&#xff1a;打开后不能正常显示&#xff0c;只有标题栏正常解决&#xff1a;用vi打开Zend_Development_Environment&#xff08;和你选择安装的路径有关&#xff0c;找下&#xff09;&#xff0c;打开后输入/set nu在输入1693在其附近会有类似下面的代码&#x…

CCRD_TOC_2008年第3期

中信国健临床通讯 2008年第3期 目 录 银屑病和银屑病关节炎 1. 国际皮肤病专家呼吁重视生物制剂治疗银屑病 原文: http://pharmatimes.com/forums/forums/t/855.aspx 2. 案例报道&#xff1a;依那西普有效治疗泛发性脓疱型银屑病 Esposito M, et al. Dermato…

Ubuntu无线上网

sudo apt-get install bcm43xx-fwcutter&#xff0c;不但自动安装了fwcutter工具&#xff08;这个工具本来是从windows driver .sys文件中分离出firmware的&#xff09;&#xff0c;而且还主动提示我要不要直接从网上下载firmware本身。回答yes就一切完成&#xff08;自动下载了…