Home javascript Go to another page by clicking on a block

Go to another page by clicking on a block

Author

Date

Category

How do I navigate to another page when I click on the HTML block? Without using the & lt; a & gt; tag.


Answer 1, authority 100%

Well, you won’t be able to make a link from div , but you can bind onclick to the desired block and make the transition like following the link as follows:

& lt; div onclick = "location.href = 'http: //yoursite.com';" & gt; Content & lt; / div & gt;

Example:

div {
 height: 200px;
 width: 200px;
 outline: 1px solid black;
 display: flex;
 align-items: center;
 justify-content: center;
}
div: hover {
 outline: 2px solid black;
 cursor: pointer;
} 
& lt; div onclick = "location.href = 'https: //ru.stackoverflow. com '; "& gt; Redirect to another site & lt; / div & gt; 

Answer 2, authority 75%

Making references to JS is already a wrong semantic approach. Links must be indexed and accessible by screen readers. And why complicate life with javascript when there is a possibility of implementation in native HTML.

Do this:

. block {
 display: block;
} 
& lt; a href = '#' class = 'block' & gt;
& lt; div class = 'block__content' & gt; Content & lt; / div & gt;
& lt; / a & gt; 

Answer 3, authority 25%

On click, for example.

$ ('div # go_to_yandex'). on ('click', function (event) {
  window.location.href = 'https://ya.ru';
});

or without Query:

document.getElementById ('go_to_yandex'). onclick = function (event) {
  window.location.href = 'https://ya.ru';
};

Answer 4, authority 25%

Add the onClick = "window.location.href = '' event to the block, add the link URL

to the href

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions