我正在做 FreeCodeCamp 的随机报价机练习。
Using 这个答案 https://stackoverflow.com/a/22962821/4956253,我尝试设置我的推文按钮来打开一个新窗口,而不是用户可以用来发布此报价的选项卡。
唯一的问题是它打开了一个新选项卡也一个新窗口。有什么办法可以让它只打开新窗口吗?
推文链接的 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));
});
我的项目的笔 http://codepen.io/DouglasSymb/pen/QEoVgw
谢谢你!
您将获得自定义行为(打开新窗口)和链接的标准行为(在当今大多数浏览器中,打开新选项卡)。为了防止后者,您需要使用“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));
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)