jQuery:转到目标为“_blank”的 URL

发布于 2024-11-19 21:39:10 字数 348 浏览 5 评论 0原文

我正在使用这段 jQuery 代码来获取链接的 href:

var url = $(this).attr('href');

-- 并使用这段代码转到该 href:

window.location = url;

一切都按照我想要的方式进行,除了新页面在与前一个页面相同的窗口中打开,并且我希望它在新窗口或选项卡中打开(在纯 html 中可以通过使用 target="_blank" 公式来实现)。

问题:如何使用 jQuery 在新窗口或选项卡中打开 href?

感谢您的帮助!

I am using this bit of jQuery code to get href of the link:

var url = $(this).attr('href');

-- and this bit of code to go to that href:

window.location = url;

Everything is just the way I want it, except the new page opens in the same window as the previous one, and I want it to open in a new window or tab (something that in plain html would have been achieved by using target="_blank" formula).

Question: How can I open the href in the new window or tab with jQuery?

Thank you for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

み零 2024-11-26 21:39:11

检测目标属性是否已使用且包含“_blank”。对于不喜欢“_blank”的移动设备,这是一个可靠的替代方案。

    $('.someSelector').bind('touchend click', function() {

        var url = $('a', this).prop('href');
        var target = $('a', this).prop('target');

        if(url) {
            // # open in new window if "_blank" used
            if(target == '_blank') { 
                window.open(url, target);
            } else {
                window.location = url;
            }
        }           
    });

Detect if a target attribute was used and contains "_blank". For mobile devices that don't like "_blank", this is a reliable alternative.

    $('.someSelector').bind('touchend click', function() {

        var url = $('a', this).prop('href');
        var target = $('a', this).prop('target');

        if(url) {
            // # open in new window if "_blank" used
            if(target == '_blank') { 
                window.open(url, target);
            } else {
                window.location = url;
            }
        }           
    });
捶死心动 2024-11-26 21:39:11

如果您想通过 jQuery 创建弹出窗口,那么您需要使用插件。这似乎会做你想做的事:

http://rip747.github.com/popupwindow/

或者,您始终可以使用 JavaScript 的 window.open 函数。

请注意,无论采用哪种方法,都必须打开新窗口以响应用户输入/操作(例如,单击链接或按钮)。否则浏览器的弹出窗口阻止程序将仅阻止弹出窗口。

If you want to create the popup window through jQuery then you'll need to use a plugin. This one seems like it will do what you want:

http://rip747.github.com/popupwindow/

Alternately, you can always use JavaScript's window.open function.

Note that with either approach, the new window must be opened in response to user input/action (so for instance, a click on a link or button). Otherwise the browser's popup blocker will just block the popup.

微暖i 2024-11-26 21:39:11

尝试使用以下代码。

$(document).ready(function(){
    $("a[@href^='http']").attr('target','_blank');
});

Try using the following code.

$(document).ready(function(){
    $("a[@href^='http']").attr('target','_blank');
});
把梦留给海 2024-11-26 21:39:11
 url='/controller/action';  
 window.open(location.origin+url,'_blank');
 url='/controller/action';  
 window.open(location.origin+url,'_blank');
一场春暖 2024-11-26 21:39:10

您需要打开一个新窗口:

window.open(url);

https://developer.mozilla.org /en-US/docs/DOM/window.open

You need to open a new window:

window.open(url);

https://developer.mozilla.org/en-US/docs/DOM/window.open

辞取 2024-11-26 21:39:10

使用,

var url = $(this).attr('href');
window.open(url, '_blank');

更新:使用 prop 检索 href 效果更好,因为它会返回完整的 url,而且速度稍快一些。

var url = $(this).prop('href');

Use,

var url = $(this).attr('href');
window.open(url, '_blank');

Update:the href is better off being retrieved with prop since it will return the full url and it's slightly faster.

var url = $(this).prop('href');
谎言 2024-11-26 21:39:10

问题:如何在新窗口或选项卡中打开 href
jQuery?

var url = $(this).attr('href').attr('target','_blank');

Question: How can I open the href in the new window or tab with
jQuery?

var url = $(this).attr('href').attr('target','_blank');
温柔女人霸气范 2024-11-26 21:39:10

.ready 函数用于在页面加载完成后插入属性。

$(document).ready(function() {
     $("class name or id a.your class name").attr({"target" : "_blank"})
})

The .ready function is used to insert the attribute once the page has finished loading.

$(document).ready(function() {
     $("class name or id a.your class name").attr({"target" : "_blank"})
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文