JQuery How to extract value from href tag?

前端 未结 6 1169
野性不改
野性不改 2020-12-01 02:20

I am new to JQuery.

If I have the following tag. What is the best JQuery method to extract the value for \"page\" from the href.



        
6条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-01 02:31

    Here's a method that works by transforming the querystring into JSON...

    var link = $('a').attr('href');
    
    if (link.indexOf("?") != -1) {
        var query = link.split("?")[1];
    
        eval("query = {" + query.replace(/&/ig, "\",").replace(/=/ig, ":\"") + "\"};");
    
        if (query.page)
            alert(unescape(query.page));
        else
            alert('No page parameter');
    
    } else {
        alert('No querystring');
    }
    

    I'd go with a library like the others suggest though... =)

提交回复
热议问题