Javascript在新的弹出窗口中打开链接

时间:2016-10-16 05:19:56

标签: javascript html

我正在使用wordpress中的主题并在帖子的底部创建内联共享按钮,但我陷入了困境。 我想用javascript打开新弹出窗口中的链接。 这是html代码。

    <ul>
  <li>
    <a href="//somesite.com/" rel="nofollow" class="social_share_link">Share</a>
  </li>
</ul>  

这里是javascript

    <script  type="text/javascript">
$("a.social_share_link").on("click",function(){
    var share_link = $(this).prop('href');
    window.open(share_link,'','scrollbars=1,height=500,width=500,left=500,top=100');
     });
</script>

请帮帮我。

4 个答案:

答案 0 :(得分:4)

这是您的工作代码:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
  <li>
    <a href="//somesite.com/" rel="nofollow" class="social_share_link">Share</a>
  </li>
</ul>

这些是您需要做的更改:

$("a.social_share_link").on("click", function() {
  var share_link = $(this).prop('href');
  console.log(share_link);
window.open(share_link, "_blank", "toolbar=yes,scrollbars=yes,resizable=yes,top=500,left=500,width=400,height=400");

});

您也可以在此处参考工作小提琴https://jsfiddle.net/9wua66mw/

答案 1 :(得分:1)

只需简单调用onclick事件

  
      
  1. 没有额外的脚本(内联)
  2.   
<a href="//somesite.com/"  class="social_share_link"
onclick="return !window.open(this.href, 'somesite', 'width=500,height=500')"
target="_blank">Share</a>
  
      
  1. 使用额外的脚本&amp; jquery`
  2.   
<a href="//somesite.com/"  class="social_share_link" target="_blank">Share</a>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
   $(".social_share_link").click(function(event) {
      event.preventDefault();
      var share_link = $(this).prop('href');
      window.open(share_link, "social_share", "width=500,height=500");
   });
</script>

答案 2 :(得分:0)

防止默认事件 - 使用

重定向到( )属性指向的任何位置
var re = /^(.{0}|.{6,})$/;

console.log('', re.test(''));
console.log('1', re.test('1'));
console.log('12', re.test('12'));
console.log('123', re.test('123'));
console.log('1234', re.test('1234'));
console.log('12345', re.test('12345'));
console.log('123456', re.test('123456'));
console.log('1234567', re.test('1234567'));
console.log('12345678', re.test('12345678'));

答案 3 :(得分:0)

您的代码看起来正确,尝试使用属性_blank或检查开发人员工具,也许您有一个您看不到的错误。 我测试了你的代码并且工作正常。

此处是{{1}} attibute的演示:https://jsfiddle.net/quethzel/L1jerwo1/2/