Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Tweet按钮打开新窗口和新选项卡_Javascript_Jquery_Twitter - Fatal编程技术网

Javascript Tweet按钮打开新窗口和新选项卡

Javascript Tweet按钮打开新窗口和新选项卡,javascript,jquery,twitter,Javascript,Jquery,Twitter,我正在做FreeCodeCamp的随机报价机练习 使用,我试图设置我的tweet按钮以打开一个新窗口,而不是一个用户可以用来tweet此引用的选项卡 唯一的问题是,它正在打开一个新选项卡以及一个新窗口。有没有办法让它打开新窗口 推特链接的HTML <a class="twitter-share-button" href="https://twitter.com/intent/tweet" target="_newwin"> Tweet</a> 谢谢大家! 您将获得自定

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

使用,我试图设置我的tweet按钮以打开一个新窗口,而不是一个用户可以用来tweet此引用的选项卡

唯一的问题是,它正在打开一个新选项卡以及一个新窗口。有没有办法让它打开新窗口

推特链接的HTML

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


谢谢大家!

您将获得自定义行为(打开新窗口)和链接的标准行为(在当今大多数浏览器中,打开新选项卡)。要防止后者,您需要使用“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));
});

据猜测,新窗口(或选项卡)由于锚定的默认行为而打开,尝试一种简单的解决方法是将
onclick=“return false;”“
添加到锚定中。这解决了我的问题,让我更清楚地了解了preventDefault()的用途,谢谢Martin!
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));
});