2016-08-22 321 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>

相關問題