Add a stay style to the navigation bar and a stay style to the navigation bar

Source: Internet
Author: User

Add a stay style to the navigation bar and a stay style to the navigation bar

Let's talk about the following three methods to add a stay style for something similar to the navigation bar:

Instance:

HTML code:

<Div class = "side_layout"> <! -- Sidebar content --> <div class = "list-group"> <a href = "#" class = "list-group-item active"> Project 1 <span class =" glyphicon_right_img glyphicon-chevron-right "> </span> </a> <a href =" # "class =" list-group-item "> Project 2 <span class = "glyphicon_right_img glyphicon-chevron-right"> </span> </a> <a href = "#" class = "list-group-item"> Project 3 <span class = "glyphicon_right_img glyphicon-chevron-right"> </span> </a> <a href = "#" class = "list-group-item"> Project 4 <span class = "glyphicon_right_img glyphicon-chevron-right"> </span> </a> <a href = "#" class = "list-group-item"> Project 5 <span class = "glyphicon_right_img glyphicon-chevron-right"> </span> </a> <a href = "#" class = "list-group-item"> Project 6 <span class = "glyphicon_right_img glyphicon-chevron-right"> </span> </a> </div>
Js Code:

Method 1:

$ (Function () {$ (". side_layout "). click (function () {$ (this ). addClass ('active'); $ (this ). siblings (). removeClass ('active') ;}) ;}; Method 2: $ (document ). on ("click ",". side_layout a ", function () {$ (this ). addClass ('active '). siblings (). removeClass ('active') ;}; method 3: $ (function () {$ (". side_layout "). bind ('click', function () {$ (this ). addClass ('active'); $ (this ). siblings (). removeClass ('active ');});});
I personally think method 2 is the most convenient.

Both method 1 and method 3 must be included in $ (function ().

Method 2 and 3 bind a click event to the element. The difference is that $ (document ). the on METHOD delegates the binding to the document, and the binary bind method delegates the binding to the document. on side_layout a, the former is more efficient when reading data ~~~





How to dynamically set the selected current style in the navigation bar

Code comment
! Important can improve the application priority of a specified style rule
Width: 111px! Important width of the entire menu
Width: 80px! Important menu connection width
Top: the position of the entire menu, that is, 234px from the top
Height: 222px! Important can adjust the height as needed
Line-height: 15px! Important and height: 15px! Important corresponds to a menu. You can also set the position arrangement rules between menus.

How to Use jquery to add a style to the navigation bar along with the page?

Click "Homepage" or "Popular recommendations". Are these navigation not jump pages? They all jump to another page. Those actions won't work, and they are determined by the background. I hope it will help you. If it is not a jump, I will help you write a JQ.

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.