Note that there are some explanatory texts on larger screens.

plurals
  1. POHighlighting a 'li' element in 'ul' with jQuery
    primarykey
    data
    text
    <p>On <a href="http://sample.srinformatics.nl" rel="nofollow">this page </a> you will see my page. I made have a menu. I wanted to make it more clear that this menu is a menu by highlighting the links on hover. While I normally would use JavaScript, jQuery sounded more simple to implement since it has <code>.hover</code>. So to highlight the elements from the list, I added this script:</p> <pre><code>&lt;script&gt; $("li").hover( function(){ $(this).css("background-color", "#525252"); }, function() { $(this).css("background-color", "#FFF"); } ); &lt;/script&gt; </code></pre> <p>In my understanding of jQuery, the first function embedded in the hover() function is run when the user is hovering over the li-element. The second function is run when the mouse leaves the li-element. <a href="http://api.jquery.com/hover/" rel="nofollow">jQuery API-link</a>. The added script however doesn't seem to do anything at all.</p> <p>My first try was to change the first line</p> <pre><code>$("li") </code></pre> <p>because this might not be the correct approach. This because it is a <code>ul</code> <code>li</code> so I tried <code>ul</code> <code>li</code> instead of just <code>li</code>.</p> <p>The second solution was to use addClass but to make another class sounded a bit redundant to me. So I decided to not use this.</p> <p>Last I tried to add an alert to see if the code is ran at all on hover. This is the case. When I added an alert in the code and hovered over the li-element the alert was not triggered. So to my understanding the faulty code is only one line:</p> <pre><code>$("li").hover() </code></pre> <p>But I haven't found what exactly is faulty here.</p> <p>The HTML code for the menu is the following:</p> <pre><code>&lt;div id="menu"&gt; &lt;ul&gt; &lt;li&gt;&lt;strong&gt;&lt;em&gt;&lt;u&gt;Main Menu&lt;/u&gt;&lt;/em&gt;&lt;/strong&gt;&lt;/li&gt; &lt;li onclick="moveDiv('home');"&gt;Home&lt;/li&gt; &lt;li onclick="moveDiv('profile')"&gt;Profile&lt;/li&gt; &lt;li onclick="moveDiv('news')"&gt;News&lt;/li&gt; &lt;li onclick="moveDiv('forums')"&gt;Forums&lt;/li&gt; &lt;li onclick="moveDiv('webshop')"&gt;Web Shop&lt;/li&gt; &lt;li onclick="moveDiv('status')"&gt;Status&lt;/li&gt; &lt;/ul&gt; &lt;/div&gt; </code></pre> <p>CSS sheet:</p> <pre><code>#menu ul li { list-style-type: none; margin-top: 10px; clear: left; cursor: pointer; } #menu ul { margin: 0px; padding: 0px; color: #525252; } #menu ul li a { text-decoration: none; color: #525252; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload