At the top of my webpage I have all of my links laid out horizontally as such:
Link 1 Link 2 Link 3 etc...
What I'm trying to do is, when you click on Link 1 for example, a border-bottom:1px solid #000 gets added to the css. If I were to click link 2, then the border-bottom on link one would be set to 'none' and the border-bottom:1px solid #000 would be set on link two.
I guess what I'm trying to ask is, how do I add an onclick handler to all of the links in my menu bar? So that, a "selected" class would be added to the link for the current page, and when I click another link, that class is removed and added onto the next link.
I've tried using javascript but I'm unable to remove the selected class after I've added it. Any suggestions on how to accomplish this?
Something like this?
<ul id="nav">
<li><a class="menu" href="#">1</a></li>
<li><a class="menu" href="#">2</a></li>
<li><a class="menu" href="#">3</a></li>
</ul>
then
$('a.menu').click(function(){
$('a.menu').removeClass("active");
$(this).addClass("active");
});
then in your css something with that active class
.active{
border-bottom: 1px solid #ccc;
}
You can use jQuery:
Add this into onclick inside that
$("#id").addClass("classname");
<style>
#id {
height: 20px;
width: 20px;
}
.classname {
display: none;
}
</style>
Link 1
<div id="id"></div>
Related
I'm looking to make a very simple dropdown menu for a navbar, very similar to how Bootstrap's dropdown menu works - without being Bootstrap (with some regular links in my navbar and some dropdown links). Essentially what I want is to come up with some with some js and probably a little bit of CSS that will enable this to happen for the following HTML code:
<ul class="main-nav">
<li>HOME</li>
<li>CONTACT</li>
<li class="dropdown">
ACCOUNT <b class="caret"></b>
<ul class="dropdown-menu">
<li>CHANGE PASSWORD</li>
</ul>
</li>
</ul>
I just don't really know where to start on something like this. I spent a few hours trying to put together an all-CSS way of doing this but my CSS just started interfering with itself and I kind of gave up on that. I don't really know any js but it strikes me that there should be a really easy way to toggle a dropdown style on and off with js by clicking a link. I even tried for quite a while to implement js dropdown scripts other people have put out and other StackOverflow answers that essentially did that but their HTML was structured significantly different than mine and I didn't know enough js to restructure their code.
At this point, I'd be more than content with the simplest way possible - a dropdown link that when clicked, opens up a single-colored rectangle 'under it' with the links stacked within in it. I know that's a lot to ask for, but if anyone could point me in the right direction, it would be much appreciated. I apologize for not showing more code but after working on this all day, I really just don't have anything useful to show for.
The idea is that the dropdown-menu is hidden using display: none and when its parent dropdown has the class open then you show it using display: block, to toggle the classes we use js.
$(document).ready(function(){
$("[data-toggle='dropdown']").click(function(e) {
$(this).parents(".dropdown").toggleClass("open"); /*when you click on an element with attr data-toggle='dropdown' it toggle the class "open" on its parent with class "dropdown"*/
e.stopPropagation();
});
$("html").click(function() {
$(".open").removeClass("open"); /*when you click out of the dropdown-menu it remove the class "open"*/
});
});
.main-nav{
background: deepskyblue;
padding: 0;
}
.main-nav li{
list-style: none;
display: inline-block;
position: relative; /*with respect to this element dropdown-menu is positioned*/
}
.dropdown-menu{
display: none; /*hide the menu*/
/*this style are just to position the menu*/
position:absolute;
top: 100%;
left: 0;
}
.open .dropdown-menu{
display: block; /*show the menu when its parent has class "open"*/
}
a.nav-item{
display: inline-block;
padding: 10px;
text-decoration: none;
cursor: pointer;
}
.dropdown-menu{
background: skyblue;
padding: 10px;
border-radius: 5px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="main-nav">
<li><a class="nav-item" href="index.html">HOME</a></li>
<li><a class="nav-item" href="contact.html">CONTACT</a></li>
<li class="dropdown">
<a class="nav-item dropdown-toggle" data-toggle="dropdown">ACCOUNT <b class="caret">></b></a>
<ul class="dropdown-menu">
<li>CHANGE PASSWORD</li>
</ul>
</li>
</ul>
The above is just a basic example to point you in the right direction, most of the CSS code is just to make viable the example, the important parts are commented.
I've tried lots and it still won't style! My css looks like this:
#subnav {
background: url(../_img/subnav.png);
height: 36px;
width: 455px;
margin: -15px 0 0 25px;
position: absolute;
}
.subnav {
font-family: verdana;
font-size: 6px;
color: #676767;
padding: 8px;
}
and my html looks like:
<div id="subnav" class="subnav">
testing 123
</div>
and it looks this way:
Side-Note: I was wondering what's the most efficient way of coding that navigation with the sub-nav? I'm kind of outdated with my html/css at the moment.
"the white bit in the picture above is my sub-nav and the blue bit is the navigation so upon click options are shown in the white bit"
LAYOUT: http://uploadir.com/uploads/v8qafb1w/downloads/new
The content of the image that you showed us is different to the content that you have present in the question.
You have sn as your class in your html and you are trying to reference subnav via css
To add a sub menu I would personally use the following.
<ul>
<li>
itsHabbo
</li>
<li>
Radio
<ul>
<li>
AM
</li>
<li>
FM
</li>
</ul>
</li>
<li>
Events
</li>
<li>
Forum
</li>
So you basically have you sub menu within the item you wish to select.
I hope this helps.
You have sn class not subnav. I changed to sn in you code and you can now see styling
<div id="subnav" class="sn"> come at me bro </div>
<div>
<a target="_blank" href="/ShowDetails.php">
<ul>
<li>Code1:123456</li>
<li>Code2:654321</li>
</ul>
... somethings ....
</a>
</div>
I want when the user clicks on Code1:123456 that they go to ShowDetails.php and when the user clicks on Code2:654321 nothing happens, so that they can copy the code. Right now when an user clicks Code2:... the user goes to ShowDetails.php too.
and i want whole div linked to ShowDetails.php expect Code2:654321
You could maybe do this:
$(function() {
$("a").on('click', function(e) {
if (e.target.innerHTML.indexOf('654321')>-1) e.preventDefault();
});
});
Of course give your 'a tag' an ID or class for the selector.
This solution works if you know the content you're looking for
WORKING EXAMPLE
If you have any questions let me know.. good luck!
If you surround all the list with the <a> tag, all the list will be considered as a link.
You want to move the link to surround only the text in the first item :
<div>
<ul>
<li><a target="_blank" href="/ShowDetails.php">Code1:123456</a></li>
<li>Code2:654321</li>
</ul>
</div>
By the way, target="_blank" is to be avoided in order to keep accessibility for people who need it. They may be troubled if they can't go on the previous page by clicking the appropriate button.
The best solution is to generate the correct html code in the first place. You can move the link inside the first li-element.
div {
border: 1px blue dotted;
}
.spacer {
background-color: #DDDDDD;
height: 100px;
border: none;
}
<div>
<ul>
<li>
<a target="_blank" href="/ShowDetails.php">Code1:123456</a>
</li>
<li>Code2:654321</li>
</ul>
<a target="_blank" href="/ShowDetails.php">
<div class="spacer"></div>
</a>
</div>
Edit: Added extra links in places where they are needed.
I'd like the functionality of the Bootstrap tabbable nav but I want to style each tab with a background image and text underneath. In fact, what I'd really like is to just put my photoshop images right in each tab and set the active state to my selected image.
I'm having a very difficult time doing this. Is it going to take a lot of custom work to get this working with this component?
I thought I could just try with some CSS but it's not giving me the correct formatting I want:
ul.nav.nav-tabs li {
display:inline-block;
background:url(../images/skypeIcon.png) no-repeat left center;
background-size:20px auto;
font-size:15px;
padding:2px 0 2px 28px
}
By the way, I'm using Bootstrap 2.3 so I can't use Bootstrap 3 Navbar Generator.
I can use a div tag inside my a tag and put whatever content I want in there.
<ul class="nav nav-tabs">
<li class="active">
<a href="#tab1" data-toggle="tab">
<div>
<img src="<%=context%>/images/defaultAvatar.png"/>
<br/>
Computer
</div>
</a>
</li></ul>
I'm using the twitter bootstrap navbar and I want the active class to change when I go to another page but it just stays on the home page.
Here is the html on the master page
<div class="collapse navbar-collapse">
<ul class="nav navbar-nav" style="font-size: medium; color: white;">
<li class="active">Home</li>
<li>Connect</li>
<li>Develop</li>
<li>Marketplace</li>
<li>
<asp:LoginView ID="LoginView1" runat="server">
<AnonymousTemplate>
</AnonymousTemplate>
<LoggedInTemplate>
Welcome <asp:LoginName ID="LoginName1" runat="server" />
</LoggedInTemplate>
</asp:LoginView>
<asp:LoginStatus runat="server"></asp:LoginStatus>
</li>
</ul>
</div>
I found an answer that said to use this
$('.navbar li').click(function (e) {
$('.navbar li.active').removeClass('active');
var $this = $(this);
if (!$this.hasClass('active')) {
$this.addClass('active');
}
e.preventDefault();
});
But I tried it and it doesn't work. I have the js files in there
<script src="jquery/src/jquery.js"></script>
<script src="js/bootstrap.js"></script>
<script src="js/offcanvas.js"></script>
And the relevant css files are there but it still doesn't do anything.
Seems like you don't use ajax to load page when clicking to link. If It is so, the problem is that after you set 'active' class, the page reloads and it is all new. You need to decide which li will be active in your backend code or detect in javascript, what page are you currently in.
UPD:
The javascript method would look something like that
if (location.pathname.match(/connect.aspx/i)) $('a[href="Connect.aspx"]').parent().addClass('active')
if (location.pathname.match(/develop.aspx/i)) $('a[href="Develop.aspx"]').parent().addClass('active')
etc.
But this is kludge pretty much.
The better solution would be adding class="active" to needed <li> when generating page. But I don't know how your pages are generated so I cannot show example. If you use just different html files, just edit them so different <li>s have active class. If you use template engine, or something else, use its methods.
I fixed this issue by overriding the bootstrap dropdown-menu class in my CSS file:
.dropdown-menu > li.active > a {
background: white !important;
color: black !important;
}
.dropdown-menu > li.active > a:hover, .dropdown-menu > li.active > a:focus {
background: $navbar !important;
color: white !important;
}