简体   繁体   中英

jQuery making a whole Div a clickable link with _blank

I am trying to create a whole Div clickable and link to anew tab. So far I have made the div clickable and send to a new link with this code

$(document).ready(function(){
        $('.wish-list').click(function(){
          location.href = 'https://link.com'
    })
  });

This works sending the page you are on to the link but I would like to do the same action but opening a new tab.

Thank you so much for any help.

您可以使用window.open

window.open("http://link.com", "_blank");

您可以使用:

window.open('url', '_blank');

window.open

use this instead of location.href

window.open(strUrl, strWindowName[, strWindowFeatures]);

Example

window.open('http://www.google.com','_blank');

You'll need to use window.open() . The snippet below won't work in the Stack Overflow snippet environment due to sandboxing, but you can see it work here :

 $(function(){ $('.link').on("click", function(){ window.open(this.dataset.url); }); }); 
 .link { cursor:pointer; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="link" data-URL="http://espn.com">ESPN</div> 

尝试这个

 window.open('url', '_blank');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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