Make only tokens available

I am working on a menu that is created using an unordered list, with each list item containing a link to another page. However, I would also like to be able to click on each marker point to open more subcategories that will also link to other pages.

In fact, I would like to be able to click once on the link and go to the correct page, but I would also like to click on the marker and expand it in a subcategory. I studied how to separate the bullet from the contents, but it doesn't seem to work in my case, most likely because I have many subcategories. Here is an example:

<li id="m2l1" class="child"> <a href="#">Y</a> <ul id="u11"> <li class="gchild"><a href="#">Y.1</a></li> <li class="gchild"><a href="#">Y.2</a></li> <li class="gchild"><a href="#">Y.3</a></li> <li class="gchild"><a href="#">Y.4</a></li> <li class="gchild"><a href="#">Y.5</a></li> <li class="gchild"><a href="#">Y.6</a></li> <li class="gchild"><a href="#">Y.7</a></li> <li class="gchild"><a href="#">Y.8</a></li> <li class="gchild"><a href="#">Y.9</a></li> </ul> </li> 

Does anyone have any suggestions on how to separate the bullet from the text in my case?

Here's the link: http://jsfiddle.net/stamblerre/XYp48/17/

Thanks!!!

+6
source share
4 answers

Your HTML is not valid. You cannot have a div inside ul . Moreover, you can greatly simplify your code by moving a separate logic for each li into one common handler.

Something like that:

Demo: http://jsfiddle.net/abhitalks/XYp48/18/

CSS

 ul { display: none; overflow: hidden; } ul:first-child { display: block; } 

JS:

 $("li").on("click", function () { $(this).children("ul").slideToggle(); return false; }); 

Edit

I intentionally left checking a , because pressing a will navigate through the corresponding pages (as indicated in your question), so expand / collapse does not matter.

However, according to your comment, if you really want to remove a at all from the handler, you can use the event target to handle li without a . Something like that:

Demo 2: http://jsfiddle.net/abhitalks/XYp48/22/

JS:

 $("li").on("click", function (e) { var $t = $(e.target); // get the event target as a jQuery object if (!$t.is('a')) { // check if the target is not an anchor $(this).children("ul").slideToggle(); return false; } // otherwise if target is anchor, then do nothing }); 
+2
source

Change your list to hide bullets, and then change your html to:

 <li class="gchild"><a href="#">&bull;</a>Y.1</li> 

Gotta do the trick.

+3
source
 <a href="#"><li class="gchild">Y.1</li></a> 
0
source

One way that worked for me: remove the bullet using li { list-style-type: none; } li { list-style-type: none; } , then add your own markers with the symbol • (alt-8 on mac). Add this symbol inside a elements, for example:

 <a href=""></a> X 

with a label now outside the element.

Hope this works for you!

0
source

Source: https://habr.com/ru/post/970898/


All Articles