1, cause
Recently, I have to implement a customized radio style at work, and we usually use the default style. Because I really can’t think of a solution, I started searching, and finally I saw a good solution, which can perfectly solve our problem. problems encountered.
2, Principle
Everyone knows that when writing a structure, radio or checkbox will be used together with the label. When the for attribute value of the label is the same as the id value of the input, click the label to select the input. Here, the label is used to overwrite it. Our input default style is by adding a background image (beautified checkbox or radio) to the label, that is, during the click process, we cannot see the default input (set z-index:-1 for the input), and Click on the label, and load different background images through different events (here is the position of changing the background image)
3. Set the default style to beautify the checkbox or radio
(1) Page structure
<form class="form" method="post"> <fieldset> <legend>Which genres do you like?</legend> <input type="checkbox" value="action" id="check-1" name="genre"><label for="check-1" class="">Action / Adventure</label> <input type="checkbox" value="comedy" id="check-2" name="genre"><label for="check-2" class="">Comedy</label> <input type="checkbox" value="epic" id="check-3" name="genre"><label for="check-3" class="">Epic / Historical</label> <input type="checkbox" value="science" id="check-4" name="genre"><label for="check-4" class="">Science Fiction</label> <input type="checkbox" value="romance" id="check-5" name="genre"><label for="check-5" class="">Romance</label> <input type="checkbox" value="western" id="check-6" name="genre"><label for="check-6" class="">Western</label> </fieldset> <fieldset> <legend>Caddyshack is the greatest movie of all time, right?</legend> <input type="radio" value="1" id="radio-1" name="opinions"><label for="radio-1" class="">Totally</label> <input type="radio" value="1" id="radio-2" name="opinions"><label for="radio-2" class="">You must be kidding</label> <input type="radio" value="1" id="radio-3" name="opinions"><label for="radio-3" class="">What's Caddyshack?</label> </fieldset> </form>
(2) jquery code (the jquery library must be introduced)
jQuery.fn.customInput = function(){ $(this).each(function(i){ if($(this).is('[type=checkbox],[type=radio]')){ var input = $(this); //get the associated label using the input's id var label = $('label[for='+input.attr('id')+']'); //get type,for classname suffix var inputType = (input.is('[type=checkbox]')) ? 'checkbox' : 'radio'; //wrap the input + label in a div $('<div class="custom-'+ inputType +'"></div>').insertBefore(input).append(input,label); //find all inputs in this set using the shared name attribute var allInputs = $('input[name='+input.attr('name')+']'); //necessary for browsers that don't support the :hover pseudo class on labels label.hover(function(){ $(this).addClass('hover'); if(inputType == 'checkbox' && input.is(':checked')) { $(this).addClass('checkedHover'); } },function(){ $(this).removeClass('hover checkedHover'); }); //bind custom event, trigger it, bind click,focus,blur events input.bind('updateState',function(){ if(input.is(':checked')){ if(input.is(':radio')){ allInputs.each(function(){ $('label[for='+$(this).attr('id')+']').removeClass('checked'); }); }; label.addClass('checked'); } else { label.removeClass('checked checkedHover checkedFocus'); } }) .trigger('updateState') .click(function(){ $(this).trigger('updateState'); }) .focus(function(){ label.addClass('focus'); if(inputType == 'checkbox' && input.is(':checked')) { $(this).addClass('checkedFocus'); } }) .blur(function(){ label.removeClass('focus checkedFocus'); }); } }); }
After introducing the jquery library and the above code, you can execute the following code
$('input').customInput();
(3) The generated outer div
If your code structure is that label and input are written in pairs, then a div will be generated outside them, as shown in the figure
(4) Set custom default style
Prepare a picture, as follows:
You may ask why the top is not at the top, but at a certain distance, because most of our input options are centered, and we use the background image of the label to simulate it, so we are trying to make the input options Display centered. In short: ico small icons must be arranged vertically, and a certain distance must be left to achieve centered display.
/* wrapper divs */ .custom-checkbox, .custom-radio { position: relative; display: inline-block; } /* input, label positioning */ .custom-checkbox input, .custom-radio input { position: absolute; left: 2px; top: 3px; margin: 0; z-index: -1; } .custom-checkbox label, .custom-radio label { display: block; position: relative; z-index: 1; font-size: 1.3em; padding-right: 1em; line-height: 1; padding: .5em 0 .5em 30px; margin: 0 0 .3em; cursor: pointer; }
These are the outermost settings, of course you can modify them yourself
/* ==默认状态效果== */ .custom-checkbox label { background: url(images/checkbox.gif) no-repeat; } .custom-radio label { background: url(images/button-radio.png) no-repeat; } .custom-checkbox label, .custom-radio label { background-position: 0px 0px; }
/*==鼠标悬停和得到焦点状态==*/ .custom-checkbox label.hover, .custom-checkbox label.focus, .custom-radio label.hover, .custom-radio label.focus { /*background-position: -10px -114px;*/ } /*==选中状态==*/ .custom-checkbox label.checked, .custom-radio label.checked { background-position: 0px -47px; } .custom-checkbox label.checkedHover, .custom-checkbox label.checkedFocus { /*background-position: -10px -314px;*/ } .custom-checkbox label.focus, .custom-radio label.focus { outline: 1px dotted #ccc; }
End: In short, I solved my problem perfectly, and I will send you a screenshot to see
The above is the entire content of this article, I hope you all like it.

实现方法:1、用“$("img").delay(毫秒数).fadeOut()”语句,delay()设置延迟秒数;2、用“setTimeout(function(){ $("img").hide(); },毫秒值);”语句,通过定时器来延迟。

区别:1、axios是一个异步请求框架,用于封装底层的XMLHttpRequest,而jquery是一个JavaScript库,只是顺便封装了dom操作;2、axios是基于承诺对象的,可以用承诺对象中的方法,而jquery不基于承诺对象。

修改方法:1、用css()设置新样式,语法“$(元素).css("min-height","新值")”;2、用attr(),通过设置style属性来添加新样式,语法“$(元素).attr("style","min-height:新值")”。

增加元素的方法:1、用append(),语法“$("body").append(新元素)”,可向body内部的末尾处增加元素;2、用prepend(),语法“$("body").prepend(新元素)”,可向body内部的开始处增加元素。

删除方法:1、用empty(),语法“$("div").empty();”,可删除所有子节点和内容;2、用children()和remove(),语法“$("div").children().remove();”,只删除子元素,不删除内容。

在jquery中,apply()方法用于改变this指向,使用另一个对象替换当前对象,是应用某一对象的一个方法,语法为“apply(thisobj,[argarray])”;参数argarray表示的是以数组的形式进行传递。

去掉方法:1、用“$(selector).removeAttr("readonly")”语句删除readonly属性;2、用“$(selector).attr("readonly",false)”将readonly属性的值设置为false。

on()方法有4个参数:1、第一个参数不可省略,规定要从被选元素添加的一个或多个事件或命名空间;2、第二个参数可省略,规定元素的事件处理程序;3、第三个参数可省略,规定传递到函数的额外数据;4、第四个参数可省略,规定当事件发生时运行的函数。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Linux new version
SublimeText3 Linux latest version
