简体   繁体   English

使整个div可点击,而不是其中的链接

[英]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. 因此,我正在制作一些网格来显示产品,并希望使整个div标签可单击,而不是仅单击其中的超链接。

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. 我发现了一些我宁愿不使用的复杂解决方案,因为我想使这些div变得可点击。

Any suggestions are very welcome! 任何建议都非常欢迎!

Not sure what complicated examples you're talking about but you will need to use something like jQuery: 不知道您在说什么复杂的例子,但是您将需要使用类似jQuery的东西:

Give the div a class: 给div一个类:

<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/ 参见http://css-tricks.com/snippets/jquery/make-entire-div-clickable/

You can make the whole div clickable instead of just content inside it. 您可以使整个div可单击,而不仅仅是其中的内容。 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 这是JS Fiddle: JSFiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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