You can do that with jQuery:
$('li > a').on('click', function(e) {
e.preventDefault();
this.style.backgroundColor = '#fdf';
});
So every time an element a
direct descendant of a li
clicked click happens two things:
- the event
click
is canceled to avoid changing page (you can also avoid this with href="#"
- the element clicked (the
this
inside that callback) receives a new background color.
Example:
$('li > a').on('click', function(e) {
e.preventDefault();
this.style.backgroundColor = '#fdf';
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
<li>texto</li>
<li>texto</li>
<li><a href>Link</a>
<ul>
<li>texto</li>
<li>texto</li>
<li><a href>Link</a>
<ul></ul>
</li>
</ul>
</li>
</ul>
Please post the code you have already tried instead of image.
– Marconi