gpt4 book ai didi

javascript - 推文按钮打开新窗口和新标签

转载 作者:行者123 更新时间:2023-11-30 11:52:41 26 4
gpt4 key购买 nike

我正在做 FreeCodeCamp 的随机报价机练习。

使用 this answer ,我尝试设置我的推文按钮以打开一个新窗口,而不是一个选项卡,用户可以使用该选项卡来发布此报价。

唯一的问题是它打开了一个新标签页以及一个新窗口。有什么方法可以让它只打开新窗口吗?

推文链接的 HTML

<a class="twitter-share-button" href="https://twitter.com/intent/tweet" target="_newwin">
Tweet</a>

相关 JavaScript

jQuery('a[target^="_newwin"]').click(function() {
var width = 500;
var height = 300;
window.open(this.href , 'newwindow', 'width=' + width + ', height=' + height + ', top=' + ((window.innerHeight - height) / 2) + ', left=' + ((window.innerWidth - width) / 2));
});

Pen of my Project

谢谢!

最佳答案

您将同时获得自定义行为(打开新窗口)和链接的标准行为(在当今大多数浏览器中,打开新标签)。为了防止后者,您需要使用“preventDefault”方法:

jQuery('a[target^="_newwin"]').click(function(e) {
e.preventDefault();
var width = 500;
var height = 300;
window.open(this.href , 'newwindow', 'width=' + width + ', height=' + height + ', top=' + ((window.innerHeight - height) / 2) + ', left=' + ((window.innerWidth - width) / 2));
});

关于javascript - 推文按钮打开新窗口和新标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39003735/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com