Table of Contents
1. Create static effects
Home Web Front-end JS Tutorial Editable drop-down box implementation code based on jquery_jquery

Editable drop-down box implementation code based on jquery_jquery

May 16, 2016 pm 04:40 PM
drop down box editable

The principle is to add a textbox to a ul to simulate a drop-down box, and use font to simulate a drop-down button.

1. Create static effects

First use css and html to make it look like it should. Here are the two I use fonts, which can be made by yourself on the icomoon website. The advantage of using fonts is that it is very convenient to position the input box, and you can also control the size, color, etc. The only disadvantage is that IE6 and IE7 cannot display this font because they do not support the :before selector, but it can be achieved through some other methods. You can try it yourself. Below is the html code

<span style="display:inline-block;position:relative" class="combox_border">
 <input type="text" class="combox_input"/><font class="ficomoon icon-angle-bottom combox_button" style="display:inline-block"></font>
 <ul style="position:absolute;top:29px;left:-1px" class="combox_select">
 <li><a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >选项一</a></li>
 <li><a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >选项二</a></li>
 <li><a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >选项三</a></li>
 <li><a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >选项四</a></li>
 </ul>
</span>
Copy after login

1. There are style and class in the tag. This style is a required attribute and must have
2. The outermost part is wrapped with span, and then given an inline-block attribute. The reason why inline elements are used is for the convenience of future layout. It is also possible to replace them with block elements, but many times block elements will be accompanied by float Floating and other styles are more troublesome to control
3. Ficomoon icon-angle-bottom is defining the font
4. The attribute position of span is relative. I plan to use ul positioning to simulate the drop-down. The position of ul is absolute. In the future, top can get the height setting of span according to jquery. Left is hard-coded
5. I added an a tag to the content in li. I just want to be lazy here. The a tag has a :hover pseudo-class. Moving it up can change the CSS, so I can write less special effects of moving it up to the content to change the style.

Below is the CSS code:

@font-face {
 font-family: 'icomoon';
 src:url('fonts/icomoon.eot&#63;-fl11l');
 src:url('fonts/icomoon.eot&#63;#iefix-fl11l') format('embedded-opentype'),
 url('fonts/icomoon.woff&#63;-fl11l') format('woff'),
 url('fonts/icomoon.ttf&#63;-fl11l') format('truetype'),
 url('fonts/icomoon.svg&#63;-fl11l#icomoon') format('svg');
 font-weight: normal;
 font-style: normal;
}
.ficomoon{font-family:'icomoon';}
.icon-angle-top:before {content: "\f102"}.icon-angle-bottom:before {content: "\f103"}
/*下面的可根据自己的实际情况做修改*/
.combox_border{border:1px solid #c2c2c2;height:28px;width:245px}
.combox_input{border:0;line-height:25px;height:25px;padding-left: 5px;width:85%;vertical-align: middle;}
.combox_button{width:12%;text-align:center;vertical-align: middle;cursor:pointer;border-left:1px solid #c2c2c2}
.combox_select{border:1px solid #c2c2c2;border-top:0;width:100%}
.combox_select li{overflow:hidden;height:30px;line-height:30px;cursor:pointer;}
.combox_select a {display: block;line-height: 28px;padding: 0 8px;text-decoration: none;color: #666;}
.combox_select a:hover {text-decoration: none;background:#f5f5f5}
Copy after login

Combox_border and other styles here can be customized, and CSS3 styles can be added to beautify them. I made a simple style here.

2. Create JS special effects

When I was doing JS, I encountered a strange problem, that is, no error would be reported in any browser, but in IE6, it would prompt an error of unset object properties. Finally, I found out that it was because of the encoding problem of the js file. , not UTF-8, just change the encoding.

First is a jquery plug-in format

(function($){
 $.fn.combox = function(options) {
 
 };
})(jQuery);
Copy after login

Then add default parameters

var defaults = { 
 borderCss: "combox_border", 
 inputCss: "combox_input", 
 buttonCss: "combox_button", 
 selectCss: "combox_select",
 datas:[]
};
var options = $.extend(defaults, options);
Copy after login

borderCss 最外面包裹的样式,就是上面的span
inputCss 输入框的样式
buttonCss 按钮的样式,就是
selectCss 下拉列表的样式
datas 下拉列表中的内容

Then there is a rendering method

this.each(function() {
var _this = $(this);
_this = _initBorder(_this);//初始化外框CSS IE6中需要有返回值
_this = _initInput(_this);//初始化输入框
_initSelect(_this);//初始化下拉列表
});
Copy after login

Dynamicly generate input boxes, buttons, drop-down boxes, attach styles and times. I put the three renderings in three functions respectively to make it clearer

       function _initBorder($border) {//初始化外框CSS
 $border.css({'display':'inline-block', 'position':'relative'}).addClass(options.borderCss);
 return $border;
 }
 
 function _initInput($border){//初始化输入框
 $border.append('<input type="text" class="'+options.inputCss+'"/>');
 $border.append('<font class="ficomoon icon-angle-bottom '+options.buttonCss+'" style="display:inline-block"></font>');
 //绑定下拉特效
 $border.delegate('font', 'click', function() {
 var $ul = $border.children('ul');
 if($ul.css('display') == 'none') {
 $ul.slideDown('fast');
 $(this).removeClass('icon-angle-bottom').addClass('icon-angle-top');
 }else {
 $ul.slideUp('fast');
 $(this).removeClass('icon-angle-top').addClass('icon-angle-bottom');
 } 
 });
 return $border;//IE6需要返回值
 }
 
 function _initSelect($border) {//初始化下拉列表
 $border.append('<ul style="position:absolute;left:-1px;display:none" class="'+options.selectCss+'">');
 var $ul = $border.children('ul');
 $ul.css('top',$border.height()+1);
 var length = options.datas.length;
 for(var i=0; i<length ;i++)
 $ul.append('<li><a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >'+options.datas[i]+'</a></li>');
 $ul.delegate('li', 'click', function() {
 $border.children(':text').val($(this).text());
 $ul.hide();
 $border.children('font').removeClass('icon-angle-top').addClass('icon-angle-bottom');//确定的时候要将下拉的icon改变
 });
 return $border;
 }
Copy after login

I added a $ symbol to the parameters in the three functions so that I can know that this is a jquery object. There are no technical difficulties in these functions. They are all very common and natural logic. You can also change the code at any time according to your different needs. The plug-in only has a total of 50 lines, which is very easy to modify.

The following is the calling plug-in:

<script type="text/javascript">
$(document).ready(function() {
 $('#combox').combox({datas:['选项一','选项二','选项三']});
})
</script>
</head>
<body>
  <span id="combox"></span>
</body>
</html>
Copy after login

It only takes one sentence, which is very convenient.

demo demo: http://demo.jb51.net/js/2014/combox_jquery/

Demo download: http://www.jb51.net/jiaoben/199034.html

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use Vue to implement an editable data table? How to use Vue to implement an editable data table? Jun 25, 2023 pm 06:20 PM

With the continuous development of front-end technology, data tables have become one of the important tools for enterprise management and data display. In daily development, sometimes it is necessary to modify or add data in the data table. At this time, it is necessary to implement an editable data table. This article will introduce how to use Vue to implement editable data tables. 1. Implementation ideas When implementing the editable data table function, we need to consider the following points: Data presentation: Render the data into the table for display and editing. Table editing: Edit data in the table.

How to implement a multi-select drop-down box in Vue How to implement a multi-select drop-down box in Vue Nov 07, 2023 pm 02:09 PM

How to implement a multi-select drop-down box in Vue. In Vue development, the drop-down box is one of the common form components. Normally, we use radio drop-down boxes to select an option. However, sometimes we need to implement a multi-select drop-down box so that users can select multiple options at the same time. In this article, we will introduce how to implement a multi-select drop-down box in Vue and provide specific code examples. 1. Use the ElementUI component library. ElementUI is a desktop component library based on Vue, which provides a rich UI.

How to use Layui to develop an editable personal schedule management system How to use Layui to develop an editable personal schedule management system Oct 25, 2023 am 11:42 AM

How to use Layui to develop a personal schedule management system that supports editability. In recent years, with the rapid development of information technology and the accelerated pace of people's lives, personal schedule management has become more and more important. In order to allow people to better manage their time and tasks, we can use Layui, a front-end UI framework based on JavaScript. It provides rich components and a concise style, and is very suitable for developing personal schedule management systems. 1. Environment preparation First, we need to prepare the development environment. make sure you

Vue mobile drop-down box scrolling problem solution Vue mobile drop-down box scrolling problem solution Jun 29, 2023 pm 11:49 PM

How to solve the mobile drop-down box scrolling problem in Vue development. With the popularity of mobile terminals, more and more web applications have begun to be developed for mobile devices. In the process of mobile development, we often encounter a problem, which is the scrolling problem of the drop-down box on mobile devices. On the traditional PC side, the scrolling of the drop-down box is controlled by the browser's default scroll bar, but on mobile devices, there is no scroll bar, so the drop-down box cannot be scrolled. This may cause the user to be unable to select all options in the drop-down box in some scenarios.

Implementation method of editable table in Vue document Implementation method of editable table in Vue document Jun 20, 2023 pm 06:43 PM

Vue.js is one of the most popular front-end frameworks at present. It uses a data-driven approach to build user interfaces and has the characteristics of two-way data binding and componentization. In the Vue.js documentation, a method for implementing an editable table is provided. This article will introduce the specific implementation steps of this method. To prepare data, you must first prepare a set of data. Here we take student information as an example. The data format can be an array, and each element contains attributes such as name, gender, age, etc. students:[{name:'Xiao Ming

How to use JavaScript to achieve drop-down box linkage effect? How to use JavaScript to achieve drop-down box linkage effect? Oct 20, 2023 pm 03:57 PM

How to use JavaScript to achieve drop-down box linkage effect? In developing web pages, drop-down box linkage is a common interactive effect. It dynamically changes the option content of another drop-down box by selecting an option in one drop-down box, thereby achieving linkage between the two. This article will introduce how to use JavaScript to achieve the linkage effect of drop-down boxes, and attach specific code examples. HTML structure First, we need to create two drop-down boxes and add id attributes to them so that JavaScr

How to use Layui to develop an e-book reader that supports editability How to use Layui to develop an e-book reader that supports editability Oct 24, 2023 am 08:08 AM

Overview of how to use Layui to develop an editable e-book reader: Layui is an easy-to-use and powerful front-end framework that provides a rich set of UI components and development tools, allowing developers to quickly build beautiful and fully functional web application. This article will introduce how to use the Layui framework to develop an e-book reader that supports editability, allowing users to perform operations such as highlighting, marking, and taking notes in the e-book. Step 1: Project preparation First, we need to prepare a basic project structure. Create a

How to use Layui to implement editable table functions How to use Layui to implement editable table functions Oct 25, 2023 am 11:27 AM

How to use Layui to implement editable table functions Layui is a classic and concise front-end UI framework with rich components and powerful functions. During the development process using Layui, we may encounter the need to implement editable table functions. This article will introduce how to use Layui's table component and form component to implement editable table functions, and provide specific code examples. 1. Introduce the Layui library. First, introduce the relevant files of the Layui library into the project. can choose

See all articles