Jquery: Powerful selector < two >

Source: Internet
Author: User

Today I followed the data to make an example, why do I feel that I do not look good in books? Well, I admit that I only know a little bit about CSS style, and I'm not going to learn it in depth, because ... The company has art!

This small example simply implements the hiding and displaying of elements, the addition and removal of the element class attribute, and the following HTML code:

<divclass="Subcategorybox"> <ul> <li><a href="#"> Canon </a><i> (30440) </i></li> <li><a href="#"> Sony </a><i> (30440) </i></li> <li><a href="#"> Samsung </a><i> (30440) </i></li> <li><a href="#"> Nikon </a><i> (30440) </i></li> <li><a href="#"> Panasonic </a><i> (30440) </i></li> <li><a href="#"> Casio </a><i> (30440) </i></li> <li><a href="#"> Fuji </a><i> (30440) </i></li> <li><a href="#"> Kodak </a><i> (30440) </i></li> <li><a href="#"> Pentax </a><i> (30440) </i></li> <li><a href="#"> Ricoh </a><i> (30440) </i></li> <li><a href="#"> Olympus </a><i> (30440) </i></li> <li><a href="#"> BenQ </a><i> (30440) </i></li> <li><a href="#"> Patriots </a><i> (30440) </i></li> <li><a href="#"> Other brand Cameras </a><i> (30440) </i></li> </ul> <br/> </div> <divclass="ShowMore"> <a href="#"><span> Show All Brands </span></a> </div>
HTML

The JS code is as follows:

<script type="Text/javascript">$ (function () {varItem = $ ('ul Li:gt (5): Not (: last)');//Select the elements in all brands that have an index greater than 5 and that do not contain the last item. Item.hide ();//Hide Elements            varBtntoogle = $ ('Div.showmore>a');//Select buttonBtntoogle.click (function () {if(item. is(": Visible"))//determines whether the selected element is hidden{item.hide (); $( This). Find ('span'). Text ("Show All Brands");//actually always do not understand the use of this, there is time to check the information$('ul Li'). Removeclass ("promoted");//Remove class style                    }                    Else{item.show (); $( This). Find ('span'). Text ("Show Lite Branding"); $('ul Li'). Filter (": Contains (' Samsung '),: Contains (' Sony '),: Contains (' Olympus ')"). AddClass ("promoted")                    }                    return false;//hyperlinks do not jump                }                )            }            ); </script>
JS

Click the button to hide the element as follows:

Click the button to display the element as follows:

In fact, today's harvest should have three points:

1, self-made once, memory more profound a bit.

2, understand what is the DOM, used to always use the document, but also know the word dom, but do not understand it, today checked a bit of information, professional explanation is: Document Object model, DOM) is a programming interface for HTML and XML documents. It provides a structured representation of the document, which can change the content and presentation of the document. My understanding of it is: similar to the HTML tags, one layer nested layer, and the outermost is the

3. The difference between find () and filter (). This is what I didn't know before, find () is looking for a matching element in a child element, and filter () filters the collection element itself.

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.