可以使用(需要jQuery的)JavaScript的下面的代碼的Click事件處理程序附加到所有鏈接與donate
類。對page1.html和page2.html
JS
$(document).ready(function() {
$('a.donate').click(function() {
// Retrieve the href value and store it in a variable.
var href = $(this).attr('href');
// Redirect a user to the desired page and passes the href value in query string.
window.location.href = 'http://example.com/donate.html?href=' + href;
// Ensure the original click event handler is not triggered.
return false;
})
});
點擊一個鏈接,用戶會被重定向到捐贈頁面,您可以解析查詢字符串並獲得href
的值(後這就是前一頁中的<a href="...">
)。
解析查詢字符串的方法有很多,但以下快速的&髒代碼應該適合您。在donate.html
JS
var location = document.location.href;
var queryString = location.split('?')[1];
// In the href variable is now what you are looking for.
var href = queryString.substr(5);
謝謝@Anton,如果你不介意的話,請給我一些小的教程,我仍然是初學者。我的意思是把這些線加起來。 –
非常感謝@ aton,但它不適合我。 –