jQuery, CSS & siblings relationship challenge

Having trouble creating a sliding menu with jQuery. The issue is that the hover css rule applied to the main navigation also affects its siblings, even though separate css rules were set for them.

I want the siblings to only have white text without a hover image.

The current code is messy as I've tried various properties to no avail.

Here's my jQuery code:

$(document).ready(function(){
$("#main-nav li a.main-link").click(function(){
    $("#main-nav li a.close").fadeIn();
    $("#main-nav li a.main-link").removeClass("active");                                                 
    $(this).addClass("active");                                      
    $("#sub-link-bar").animate({
        height: "40px"                     
    });
    $(".sub-links").hide();
    $(this).siblings(".sub-links").fadeIn();
});
$("#main-nav li a.close").click(function(){
    $("#main-nav li a.main-link").removeClass("active");                                                                                     
    $(".sub-links").fadeOut();
    $("#sub-link-bar").animate({
        height: "1px"                      
    });     
    $("#main-nav li a.close").fadeOut();
});

HTML

<div id="sub-link-bar"></div>
 <div id="main-handle">
  <ul id="main-nav">
   <li><a class="main-link" href="#">Main Link</a>
    <ul class="sub-links">
     <li><a href="#" title="test"> Sub Link</a></li>
    </ul>
   </li>
 </ul>
</div>

And my CSS

#main-nav {
margin: 0px 0px 0px 0px;
padding-top: 10px;
padding-left: 0px;
background-image: url(../images/menu.png);
background-repeat: repeat-x;
height: 41px;
width: 100%;
list-style:none;
color:#306295;
}

#main-handle {
width: 100%;
float: right;
margin-top: -1px;
}
#main-nav li {
display: inline;
list-style: none;
}
#main-nav li a {
font-size: 12px;
text-decoration: none;
font-family:Verdana, Geneva, sans-serif;
font-weight:bold;
outline: 0;
height: 41px;
color:#306295;
text-align: left;
padding:13px 16px 13px 16px;
}
#main-nav li a:hover, #main-nav li a.active {
background-image: url(../images/bg-main-nav-hover.jpg);
text-align: left;
padding:13px 16px 13px 16px;
color:#FFF;
height: 41px;
background-position: center;
}
#sub-link-bar {
background: #476192;
background-image: url(../images/sub-bar-bg.png);
background-repeat: repeat-x;
min-height: 1px;
border-bottom: #645546 1px solid;
}
.sub-links {
display:none;
position: absolute;
width: 100%;
top: 113px;
text-align: left;
left: -40px;
}
.sub-links ul li a{
color:#FFF;
font-family: Verdana, Geneva, sans-serif;
font-size: 12px;
font-weight: normal;
background-image: none;
}
.sub-links li a:hover{
color: #e0e0e0;
text-decoration:none;
background-image: none;
}
#main-nav li a.close{
display: none;  
position: absolute;
color:#fff;
}
#main-nav li a.close:hover{
background: #900;
}

Answer №1

If you're looking to specifically target a direct child with a rule in CSS, you can use the following syntax:

.parent > .child

For example, in your code snippet:

#mainNav li > a:hover {

}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Turn off autocomplete feature to prevent interference while handling database information

My Kindo AutoComplete is nested within a helper function. Whenever I perform a search and select one of the AutoComplete results, it triggers a function that retrieves data from the source. The information takes some time to load and appear on the page. ...

Include a specific identification code in the <tr> element using ajax

I am currently using AJAX to dynamically generate table rows. I need help adding a serial number to each row, such as 'td1'. Everything else is functioning correctly, but I am struggling to implement the serial number. Any assistance would be gre ...

The event.preventDefault() method does not work on Android tablets when using touchstart

I have implemented a responsive drop-down menu that involves canceling the click event for tablet users in order to display the sub-menu using event.preventDefault(). This function works perfectly on iPad devices but seems to be ineffective on Android. E ...

Implementing the display of JSON data in a jQuery autocomplete textbox

I am struggling with loading autocomplete text box using JSON data generated by a PHP file. ["Health Infoway","Canada Health Infoway","Infowiki","Info",......"Canada"] Below is the jQuery script I am using to call getorgname.php and retrieve the JSON dat ...

There is a lack of definition for an HTML form element in JavaScript

Encountering an issue with a HTML form that has 4 text inputs, where submitting it to a Javascript function results in the first 3 inputs working correctly, but the fourth being undefined. Highlighted code snippet: The HTML section: <form action="inse ...

posting data and redirecting fails when using an ajax button

I am encountering an issue where I click a button on a page to navigate to another page with posted data, but it is redirecting without posting anything. Both $_POST and $_SESSION variables are empty. Below is my ajax function: function ajax4(Div_Submit, ...

Unable to retrieve the value of an option element within a select tag using Selenium

I am having difficulty in choosing an option from a dropdown menu while using Selenium with Python. Displaying below is the HTML code snippet: <select class="hm-input hm-dropdown" type="text" id="HME-10-widget_calendar_1" ...

The printout of the webpage is not aligning properly on an A4 page

Currently, I have been utilizing the JavaScript function window.print() to print a webpage that contains a header and footer. The height of this webpage is dynamic, as it is dependent on the content of an HTML table within the page. However, I have encou ...

Is it possible to integrate Laravel as the backend for an already existing HTML website template?

I recently designed an HTML template for a salon listing directory site, and while it looks great, I realize I also need a backend solution to manage listings, add advertisements, and more. Can I seamlessly integrate a Laravel-based admin panel into my c ...

Showing text in a textarea while maintaining formatting (such as using <br>)

Can someone help me with formatting a block of text like this: hello <br> how is your day <br> 123 To look like this: hello how is your day 123 I also need to display it in a textarea field. I attempted to do so using the following code: $ ...

Tips for showcasing HTML code retrieved from a fetch request in a Vue component

Trying to integrate HTML code received from an API into my VueJS code. The API provides the following: <p>blah blah blah</p> <ul> <li> 1 </li> <li> 2 </li> </ul> Saving this code snippet into a variable, but ...

Failure to address incomplete information results in a negative response

Is it possible to set up an alert when a title is not found? Can Axios be used for this functionality? https://i.sstatic.net/4OUuX.png async function checkForTitle(title) { title = encodeURIComponent(title); return axios.get(`http://www.omdbapi.com/ ...

The perfect timing for incorporating a JavaScript MVC framework into your project

As part of my job at a small web agency specializing in web applications for startups, I'm urging my boss to invest more in developing strong client-side frameworks using Javascript MVC standards like BackboneJS and templating with Underscore. I unde ...

Repeated ng without any HTML elements this time

I'm trying to achieve the following output: Line 1<br> Line 2<br> Line 3<br> Line 4<br> Line 5<br> I want to use ng-repeat to display these lines with only <br> separating them. ...

Which font, Helvetica or Verdana, has optimal support across all popular browsers?

When it comes to web design, the age-old debate between Helvetica and Verdana arises. Both fonts render differently on major browsers like IE and FF, making it difficult to determine which is better supported. So, which font is the best choice for ensurin ...

Ways to conceal ad container when ad space remains empty

After setting up my ad HTML like this: <div class="ad-container"> <p>Ad slot #1</p> <div id="ad-slot-1" class="ad-slot"></div> </div> I implemented the code below to collapse the ad if ...

Looking to remove any saved autofill data from your browser? Learn how to do this using either jQuery, HTML,

I've been struggling to remove the autofill data in my browser for days now, but haven't had any luck. I have attached a reference file for your review. Click here to view the attachment ...

Aligning Page to a Specific Element Without Changing the DOM or Utilizing References

My goal is to easily scroll to a specific element using #: <a href="#element">Element</a> <div name="element" /> While this method works effectively, it always takes me to the top of the element. I would prefer to have the element cent ...

aligning JSON information with JavaScript object

I am currently in the process of setting up a sample dataset in JSON format for a JavaScript tutorial that I'm going through. Here's how the data object looks in JavaScript: app.Book = Backbone.Model.extend({ defaults: { coverImage: ...

Malfunctioning navigation buttons in the owlCarousel slider

I am using the Owl Carousel slider from . However, when I click the next button four times, it shows me the fourth element in the slider instead of moving to the next slide. The navigation buttons and dots on the carousel do not seem to work correctly. ...