Make A List Item Clickable (HTML/CSS)

后端 未结 8 1904
北恋
北恋 2020-12-03 05:26

So I\'m trying to make each list-item on my site clickable but I\'m not sure what is the best way to do it. Please help me out.

So here is the relevant HTML:

相关标签:
8条回答
  • 2020-12-03 06:06

    The key is to give the anchor links a display property of "block" and a width property of 100%.

    Making list-items clickable (example):

    HTML:

    <ul>
        <li><a href="">link1</a></li>
        <li><a href="">link2</a></li>
        <li><a href="">link3</a></li>
    </ul>
    

    CSS:

    ul {
      list-style-type: none;
      margin: 0;
      padding: 0;
    }
    ul li a {
      display: block;
      width: 100%;
      text-decoration: none;
      padding: 5px;
    }
    ul li a:hover {
      background-color: #ccc;
    }
    
    0 讨论(0)
  • 2020-12-03 06:11

    Here is a working solution - http://jsfiddle.net/STTaf/

    I used simple jQuery:

    $(function() {
        $('li').css('cursor', 'pointer')
    
        .click(function() {
            window.location = $('a', this).attr('href');
            return false;
        });
    });
    
    0 讨论(0)
提交回复
热议问题