我正在做FreeCodeCamp的随机报价机练习。
使用此答案,我尝试设置我的tweet按钮来打开一个新窗口,而不是一个用户可以用来对引用进行鸣叫的选项卡。
唯一的问题是它正在打开一个新选项卡以及一个新窗口。有什么办法可以让我打开新窗口吗?
Tweet链接的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));
});
谢谢!
您将同时获得自定义行为(打开一个新窗口)和链接的标准行为(如今在大多数浏览器中,打开一个新选项卡)。为了防止后者,您需要使用“ 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));
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句