![](/img/trans.png)
[英]Jquery to modify all anchor tag href on a page and extract key request parameters from href attribute
[英]Extract values from href attribute string using JQuery
我想使用JQuery從href屬性字符串中提取值
$(this).attr("href")
會給我的
?sortdir=ASC&sort=Vendor_Name
我需要的是將這些值解析為數組
myArray['sort']
myArray['sortdir']
有任何想法嗎? 謝謝!
順便說一句,我在其他地方看到了以下與查詢字符串一起使用的類似想法。 我還不能根據我的需要調整它。
var urlParams = {};
(function () {
var match,
pl = /\+/g, // Regex for replacing addition symbol with a space
search = /([^&=]+)=?([^&]*)/g,
decode = function (s) { return decodeURIComponent(s.replace(pl, " ")); },
query = window.location.search.substring(1);
while (match = search.exec(query))
urlParams[decode(match[1])] = decode(match[2]);
})();
請嘗試以下方法:
var href = $(this).attr("href")
href = href.replace('?', "").split('&');
var myArr = {};
$.each(href, function(i, v){
var s = v.split('=');
myArr[s[0]] = s[1];
});
也許有更好的解決方案,但要快點我應該做那樣的事情
var url = $(this).attr("href");
url = url.replace("?sortdir=", "");
url = url.replace("sort=", "");
myArray['sort'] = url.split("&")[1]; // Or use a temporary tab for your split
myArray['sortdir'] = url.split("&")[0];
該解決方案取決於您的網址是否仍然像?sortdir = ASC&sort = Vendor_Name
你可以從這里使用jQuery BBQ的deparam函數:
http://benalman.com/code/projects/jquery-bbq/examples/deparam/
嘗試這個
function getURLParameter(name, string) {
return decodeURI(
(RegExp(name + '=' + '(.+?)(&|$)').exec(string)||[,null])[1]
);
}
var string = $(this).attr("href");
alert(getURLParameter("sort",string));
在這里演示http://jsfiddle.net/jbHa6/您可以更改var字符串值並進行播放。
編輯
刪除了第二個示例,因為該代碼不是那么好,不能達到目的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.