How do I make the whole area of a list item in my navigation bar, clickable as a link?

前端 未结 10 1915
栀梦
栀梦 2020-11-27 13:50

I\'ve got a horizontal navigation bar made from an unordered list, and each list item has a lot of padding to make it look nice, but the only area that works as a link is th

相关标签:
10条回答
  • 2020-11-27 14:25

    Put the list item within the hyperlink instead of the other way round.

    For example with your code:

    <a href="#"><li>One</li></a>
    
    0 讨论(0)
  • You should use this CSS property and value into your li:

    pointer-events:all;
    

    So, you can handle the link with jQuery or JavaScript, or use an a tag, but all other tag elements inside the li should have the CSS property:

    pointer-events:none;
    
    0 讨论(0)
  • 2020-11-27 14:33

    Or you could use jQuery:

    $("li").click(function(){
       window.location=$(this).find("a").attr("href"); 
       return false;
    });
    
    0 讨论(0)
  • 2020-11-27 14:34

    Just simply apply the below css :

    <style>
      #nav ul li {
        display: inline;
      }
    
      #nav ul li a {
        background: #fff;// custom background
        padding: 5px 10px;
      }
    </style>
    
    0 讨论(0)
提交回复
热议问题