How to do <li><div class="class" onclick="window.Location='link';"></div> open in new tab?

Asked

Viewed 1,488 times

4

I’m trying to open a link in div in a new tab, but target="_blank" is not working.

 Eu queria abrir o link numa nova aba.

<li>
    <div class="clientes" target="_blank" onclick="window.location='LINK';">
        TEXTO
    </div>
</li>

Someone could help me?

  • 1

    The attribute target is for anchors... why don’t you use <div><a target="_blank" href="o-teu-link.com">texto</a></div> which is the correct semantics?

  • I’m just a kid trying to do a few things:

  • Sergio, when changing the color of the <a mute all <a of the page, why I did not use.

  • Change the color to make it clear to the reader that it is a link... but you can change the color to what you want. But do what you wanted to open in another tab?

4 answers

3

You can use a tag to link to urls. To open in new tab:

<a target="_blank" href="http://google.com">Google</a>

But if for some reason you really need it that way, you can use:

<li>
    <div class="clientes" onclick=" window.open('http://google.com','_blank')">
        TEXTO
    </div>
</li>
  • Thanks for helping me :)

  • Just remember to use _blank on external pages is dangerous, the target page will have to manipulate the source page (via window.opener), see this for more information or this. If you use the _blank use the noreferrer and noopener together.

  • Thanks Aline, that was it :)

3

The attribute target is for anchors and does not work on elements div.

You must use <div><a target="_blank" href="o-teu-link.com">texto</a></div> which is the correct semantics. If you want to change color you can use CSS.

Example:

.clientes a {
  text-decoration: none;
  color: darkgreen;
}
<div class="clientes"><a target="_blank" href="o-teu-link.com">texto A</a></div>
<div class="clientes"><a target="_blank" href="o-teu-link.com">texto B</a></div>

1

With the code that is in the question it is necessary javascript to open the page so the code is.:

<li>
   <div class="clientes"  onclick="window.open('LINK');">text</div>
</li>

With the questions I think what you want is to tag the one that will look like this.:

<li>
   <a target="_blank"  href="LINK">text</a>
</li>

0

Thank you to all(s) who replied. I’m sorry if I was a little annoying, since I only know the basics of the basics. Thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.