简体   繁体   English

使所有div可点击,并在空白处打开链接

[英]Make all div clickable and open link in a blank

I am tring to make a entire div clickable and to open the link in a blank page: 我想使整个div可点击,并在空白页中打开链接:

HTML code: HTML代码:

<div class="article-content">
    <div>
        <p></p>
    </div>
    <div>
        <p></p>
    </div>
     <div><a href="http://www.google.ro" target="_blank">Click me</div>

</div>

JS: JS:

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

But every the link is opened in the same windows. 但是每个链接都在同一窗口中打开。

have any ideea how to fix this ? 有任何想法如何解决此问题?

You need to use window.open() to open a new window 您需要使用window.open()打开一个新窗口

$(".article-content").click(function () {
    window.open($(this).find("a").attr("href"), '_blank');
    return false;
});

You need to use window.open with target set to blank. 您需要使用window.open并将目标设置为空白。 link will be opened in popup window or new tab based on browser setting in users machine: 链接将根据用户计算机中的浏览器设置在弹出窗口或新选项卡中打开:

$(".article-content").click(function () {
 window.open($(this).find("a").attr("href"),'_blank');
 return false;
});
    $(".article-content").click(function () {
        window.open($(this).find("a").attr("href"), '_blank');
        return false;
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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