Turn the div into a link

I googled how to turn a div into a link, but there seem to be a lot of methods, none of which seem to work well in IE.

What is the best / easiest way to turn a div into a link?

0
source share
4 answers

Why not use a binding and show it as a block element?

a { 
   display: block; 
   //remaining code here
}
+3
source

What does "link" mean? You can do it:

#mydiv {
  color: #00f;
  cursor: pointer;
}
#mydiv:hover {
  color: #f0f;
}
0
source

?

<div onClick="window.location = 'http://www.cannonade.net';">blah</div>
0

JavaScript:

<div onclick="alert('You clicked me !')">Click Me</div>

JQuery

$('#div_id').click(function(){
  alert('Clicked !!');
});

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

JQuery

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

The above code cancels the default link action (link to link) with return falseand binds the event clickwith the div to the class myBox, then it finds the link attribute srcinside the div and is window.locationused to redirect the page to the srclink attribute present inside the div. Thus, this basically makes the div interactive.

0
source

Source: https://habr.com/ru/post/1792365/


All Articles