[英]anchor tag in not making div clickable
i am trying to make a whole div clickable, i did the following 我试图使整个div可点击,我做了以下工作
<a href=""><div class="col-lg-3 col-md-6 col-sm-6 col-12 item-mb"> <div class="service-box1 bg-body text-center"> <img src="img/service/service1.png" alt="service" class="img-fluid"> <h3 class="title-medium-dark mb-none"><a href="category-list-layout1.html">Electronics</a></h3> <div class="view">(19,805)</div> </div> </div></a>
but he div is not becoming clickable, can anyone please tell me whats wrong in my code? 但他的div无法点击,有人可以告诉我我的代码有什么问题吗?
You can use javascript, in the code below I created a function that adds click
event handlers instead of using a
tags which can't be put inside each other. 您可以使用javascript,在下面的代码中,我创建了一个添加click
事件处理程序的函数,而不是使用不能放在彼此之间a
标签。
Also included an example of how to style the div (see clickable
css class). 还包括一个有关如何设置div样式的示例(请参见clickable
css类)。
function addLink(divId, url){ let div = document.getElementById(divId); div.setAttribute('data-link-url', url); link1.addEventListener('click', function(e){ window.location = this.getAttribute('data-link-url'); }); } addLink('link1', 'https://stackoverflow.com');
.clickable{ cursor: pointer; }
<div id="link1" class="clickable"> <div class="service-box1 bg-body text-center"> <img src="img/service/service1.png" alt="service" class="img-fluid"> <h3 class="title-medium-dark mb-none">text</h3> <div class="view">(19,805)</div> </div> </div>
First of all, you can't have a div
inside a
. 首先,你不能有一个div
里面a
。 I mean, technically in some environments it works, but its against specification, so you can't rely on that structure. 我的意思是,从技术上讲,它在某些环境中有效,但是它违反规范,因此您不能依赖该结构。 If you want to have a div
wide link you better have your a
absolutely positioned and stretched to fit your div
如果您想使用div
广泛链接,最好将a
绝对定位并拉伸以适合div
Try
<div class="col-lg-3 col-md-6 col-sm-6 col-12 item-mb" onClick="javascript:window.location='//stackoverflow.com'"> <div class="service-box1 bg-body text-center"> <img src="img/service/service1.png" alt="service" class="img-fluid"> <h3 class="title-medium-dark mb-none"><a href="category-list-layout1.html">Electronics</a></h3> <div class="view">(19,805)</div> </div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.