2016-08-22 26 views
3

は2つのURLを持つアンカータグを作成しました。 1つはhrefで、もう1つは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>

関連する問題