简体   繁体   中英

Make the whole div clickable instead of the links inside of it

So i'm making some grids to show products in and would like to make the whole div tag clickable instead of just the hyperlinks inside of it.

What would be the best and easiest way to do this. I found some complicated solutions which i rather not use since i have a lot of these divs i want to make clickable.

Any suggestions are very welcome!

Not sure what complicated examples you're talking about but you will need to use something like jQuery:

Give the div a class:

<div class="myBox">
     blah blah blah.
    <a href="http://google.com">link</a>
</div>

and in page load, hook up the handler with a selector:

$(".myBox").click(function(){
     window.location=$(this).find("a").attr("href"); 
     return false;
});

See http://css-tricks.com/snippets/jquery/make-entire-div-clickable/

You can make the whole div clickable instead of just content inside it. Just put like this :

<a href="http://www.google.com"><div id="mydiv"> Blah Blah </div></a>

That's it. Here is JS Fiddle : JSFiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM