Cómo hacer que el enlace HTML activado haciendo clic en el?


Tengo el siguiente marcado,

<ul id="menu">              
    <li><a href="#">Something1</a></li>
    <li><a href="#">Something2</a></li>
    <li><a href="#">Something3</a></li>
    <li><a href="#">Something4</a></li>
</ul>

El <li> es un poco grande, con una pequeña imagen a su izquierda, Tengo que hacer clic en el <a> para activar el enlace. ¿Cómo puedo hacer un clic en el <li> activar el enlace?

Edit1:

ul#menu li
{
    display:block;
    list-style: none;
    background: #e8eef4 url(images/arrow.png) 2% 50% no-repeat;
    border: 1px solid #b2b2b2;
    padding: 0;
    margin-top: 5px;
}

ul#menu li a
{

    font-weight: bold;
    text-decoration: none;
    line-height: 2.8em;
    padding-right:.5em;
    color: #696969;
}
Author: Mateusz Piotrowski, 2009-07-14

11 answers

#menu li { padding: 0px; }
#menu li a { margin: 0px; display: block; width: 100%; height: 100%; }

Puede necesitar algunos ajustes para IE6, pero eso se trata de cómo lo haces.

 104
Author: MiffTheFox,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-07-13 20:08:23

Como dijo Marineio, puedes usar el atributo onclick del <li> para cambiar location.href, a través de javascript:

<li onclick="location.href='http://example';"> ... </li>

Alternativamente, puede eliminar cualquier margen o relleno en el <li>, y agregar un relleno grande al lado izquierdo del <a> para evitar que el texto pase por encima de la viñeta.

 17
Author: Eric,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-07-13 20:16:47

Solo para lanzar esta opción:

<ul id="menu">
    <a href="#"><li>Something1</li></a>
    <a href="#"><li>Something2</li></a>
    <a href="#"><li>Something3</li></a>
    <a href="#"><li>Something4</li></a>
</ul>

Este es el estilo que uso en mis menús, hace que el elemento de la lista en sí mismo sea un hipervínculo (similar a como uno haría una imagen un enlace).
Para el estilo, suelo aplicar algo como esto:

nav ul a {
    color: inherit;
    text-decoration: none;
}

Luego puedo aplicar cualquier estilo a la

  • que desee.

    Nota: Los validadores se quejarán de este método, pero si eres como yo y no basas tu vida en ellos, esto debería funcionar bien.

  •  12
    Author: Duncan,
    Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
    2013-07-05 14:03:24

    Simplemente agregue envolver el texto del enlace en una etiqueta ' p ' o algo similar y agregue margen y relleno a ese elemento, de esta manera no afectará la configuración que MiffTheFox le dio, es decir,

    <li> <a href="#"> <p>Link Text </p> </a> </li>
    
     9
    Author: Adam,
    Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
    2012-02-20 16:25:54

    Esto hará que todo <li> objeto como un enlace:

    <li onclick="location.href='page.html';"  style="cursor:pointer;">...</li>
    
     7
    Author: DejanR,
    Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
    2011-11-06 22:54:53

    Jqyery esta es otra versión con jquery un poco menos corto. suponiendo que el elemento <a> está dentro del elemento <li> de

    $(li).click(function(){
        $(this).children().click();
    });
    
     2
    Author: Mijail,
    Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
    2014-01-16 02:14:27

    Lo siguiente parece funcionar:

    ul#menu li a {
        color:#696969;
        display:block;
        font-weight:bold;
        line-height:2.8;
        text-decoration:none;
        width:100%;
    }
    
     1
    Author: Sinan Ünür,
    Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
    2009-07-13 20:32:31

    O puedes crear un enlace vacío al final de tu <li>:

    <a href="link"></a>
    
    .menu li{position:relative;padding:0;}
    .link{
         bottom: 0;
         left: 0;
         position: absolute;
         right: 0;
         top: 0;
    }
    

    Esto creará un <li> clicable completo y mantendrá su formato en su enlace real. Podría ser útil para <div> etiqueta también

     1
    Author: MrFlo,
    Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
    2014-08-30 00:01:18

    Puedes probar un evento "onclick" dentro de la etiqueta LI, y cambiar la ubicación.href " como en javascript.

    También puede intentar colocar las etiquetas li dentro de las etiquetas a, sin embargo, esto probablemente no sea HTML válido.

     0
    Author: Marineio,
    Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
    2009-07-13 20:09:00

    ¿Cómo activar el enlace HTML haciendo clic en el

  • ?
  • Haciendo que su enlace sea tan grande como su li: simplemente mueva la instrucción

    display: block;
    

    De li a a y ya está.

    Es decir:

    #menu li
    {
        /* no more display:block; on list item */
    
        list-style: none;
        background: #e8eef4 url(arrow.gif) 2% 50% no-repeat;
        border: 1px solid #b2b2b2;
        padding: 0;
        margin-top: 5px;
    }
    
    #menu li a
    {
        display:block; /* moved to link */
        font-weight: bold;
        text-decoration: none;
        line-height: 2.8em;
        padding-right:.5em;
        color: #696969;
    }
    

    Nota al margen: puede eliminar "ul" de sus dos selectores: #menu es una indicación suficiente excepto si necesita dar peso a estas dos reglas para anular otras instrucciones.

     -1
    Author: FelipeAls,
    Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
    2009-07-13 20:42:55

    Use jQuery para no tener que escribir javascript en línea en el elemento <li>:

    $(document).ready(function(){
        $("li > a").each(function(index, value) {
            var link = $(this).attr("href");
            $(this).parent().bind("click", function() {
                location.href = link;
            });
        });
    }); 
    
     -2
    Author: Ignacio Pascual,
    Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
    2013-05-30 23:49:54