You can do this with jQuery:
$('li > a').on('click', function(e) {
e.preventDefault();
this.style.backgroundColor = '#fdf';
});
So every time a a
pendent element of a li
is clicked two things happen:
- event
click
is canceled to avoid changing page (you can also avoid this with href="#"
- The clicked element (
this
within this callback) gets a new background.
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>