2016-08-22 202 views
3

已经做了2个网址的锚标签。一个是href,另一个是data-url。当我点击按钮时,它应该在新选项卡中打开一个正确的页面。但另外我想要的是用我设置的data-url链接重新加载当前页面。window.location.href不适用于mac safari <a>?

因此,在示例中,我将href设置为google.com,并在新选项卡中打开。它是正确的,但是当我们点击按钮时,我还需要将当前页面重新加载到youtube.com的data-url。

HTML

<a href="https://www.google.com/" target="_blank" data-url="https://www.facebook.com/" id="agree">Click me</a> 

JS

$('#agree').on('click', function() { 
    var url = $(this).data('url'); 
    window.location.href = url; 
}); 

Link To Codepen

回答

0

试试这个请:d

jQuery(function(){ 
 
\t jQuery('#agree').on('click', function(e) { 
 
\t \t e.preventDefault(); 
 
\t \t var redirectUrl = jQuery(this).attr('href'); 
 
\t \t var url = jQuery(this).data('url'); 
 
\t \t window.location.href= redirectUrl; 
 
\t \t window.open(url, '_blank'); 
 
\t }); 
 
});
#agree { 
 
    background: red; 
 
    padding: 10px; 
 
    color: #fff; 
 
    display: inline-block; 
 
    text-decoration: none; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<a href="https://www.google.com/" data-url="https://www.facebook.com/" id="agree">Click me</a>

相关问题