我試圖在頁面上的所有鏈接上追加一個查詢字符串,我已經添加了該代碼來完成它,但正如您所看到的,它不起作用 http://yamputours.staging.wpengine.com/?why=broken
var querystring = location.search;
console.log(querystring);
$('a').each(function() {
console.log('working');
var href = $(this).attr('href');
if (href) {
href += (href.match(/\?/) ? '&' : '?') + querystring;
$(this).attr('href', href);
}
});
我沒有從第二個console.log()
控制檯中得到任何東西。
我知道它裏面的代碼仍然需要修改才能在href='#'
之類的東西上不被破解,但即使我刪除了除console.log('working);
之外的函數中的所有代碼,也不會從它得到任何東西。這讓我瘋狂,誰能看到我失蹤的東西?
僅供參考在這個網站上還有很多其他的jQuery,甚至在同一個文件中,所有的工作都很好,只是沒有這個功能。
您是否嘗試過使用'jQuery',而不是'$'? – 2017-08-05 14:49:41
全東西在'(function($){....}(jQuery))包裹的.js文件中;' – mrben522
將它移出並嘗試使用'jQuery('a')'以防萬一。 – mrben522