JS Custom Modify check box single box style, clear check box the default style of radio box

Source: Internet
Author: User

Before doing the project, also encountered the need to follow the design draft <input type="checkbox"> and <input type="radio"> the default style to modify, but found that there is no way to reset the effect, previously used-webkit-appearance method, but this is only in the WebKit kernel browser in effect, Firefox does not take effect.
So I wrote a JS, with Li to simulate the check box and the effect of the radio box, very simple, 7 lines on the line.


Knowledge points involved: Custom attributes to store the click state and the original class name

HTML content:

<DivId="Box" ><P> your favorite Sport</P><Ul><Li> Mountain Climbing</Li><Li> Bike</Li><Li> Swimming</Li><Liclass= ' Hong ' > Basketball </li> <li class=< Span class= "Hljs-value" > ' Hong ' > Soccer </li> <li> badminton </ li> <li> Jump Rope </li> < li> run </li> </ul></ DIV>             

JS content:
Comments more, is to explain clearly, may be a bit messy, can read the comments can be deleted

var aLi = Document.queryselectorall (' #box ul Li ');/* Get all of the LI, if you want to use the class, can be written as Document.queryselectorall ('. class ') */for (var i=0;i<ali.length;i++) {Ali[i].ifcheck =False/* The custom attribute is used to indicate that there is no selection, initialization set to FALSE, unchecked */ali[i].nowclass = Ali[i].classname;/* Custom attributes are used to store the original classname, such as the class name in the HTML content, Hong, so that after adding the class name on the back, it will not cause the original class name to be overwritten directly */Ali[i].onclick =function() {if (this.ifcheck) {/ * When the current Li is clicked, if Ifcheck is already selected */ this.classname =  This.nowclass /* makes the current click of the Li's class name equal to the original name, which means to remove the selected on class name */} else{ This.classname + = ' on ' / * If unchecked, add on to indicate that the selected */}/ * can be written as a three-mesh style this.classname = this.ifcheck?this.nowclass:this.classname+ ' on ';  This.ifcheck =! This.ifcheck; / * Start unchecked, click to select, or vice versa, so you want to give Ifcheck properties to counter */};};            

CSS style:

#box{Width600px;Margin50px Auto;}#boxP{Font-size:14px;Font-weight:BoldBorder-bottom:1px dashed#000;Line-height:30px;}#boxUl{Margin-top:10px;OverflowHidden}#boxUlLi{Width67px;Height20px;FloatLeftBackground-image:URL (images/ck.png);/* Background image when not selected */Background-repeat:No-repeat;Font-size:12px;Line-height:20px;Text-indent:30px;margin-right:8px; cursor:pointer;}  #box ul li.on{background-image: url (images/cked.jpg); /* selected time background map */} #box ul li.hong{ Span class= "Hljs-rule" >color:red;}   

JS Custom Modify check box single box style, clear check box default style

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.