JQuery How to extract value from href tag?

前端 未结 6 1149
野性不改
野性不改 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

    I see two options here

    var link = $('a').attr('href');
    var equalPosition = link.indexOf('='); //Get the position of '='
    var number = link.substring(equalPosition + 1); //Split the string and get the number.
    

    I dont know if you're gonna use it for paging and have the text in the <a>-tag as you have it, but if you should you can also do

    var number = $('a').text();
    
    0 讨论(0)
  • 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... =)

    0 讨论(0)
  • 2020-12-01 02:33

    Use this jQuery extension by James Padoley

    0 讨论(0)
  • 2020-12-01 02:43
    if ($('a').on('Clicked').text().search('1') == -1)
    {
        //Page == 1
    }
    else
    {
        //Page != 1
    }
    
    0 讨论(0)
  • 2020-12-01 02:44

    First of all you need to extract the path with something like this:

    $("a#myLink").attr("href");
    

    Then take a look at this plugin: http://plugins.jquery.com/project/query-object

    It will help you handle all kinds of querystring things you want to do.

    /Peter F

    0 讨论(0)
  • 2020-12-01 02:45

    The first thing that comes to my mind is a one-liner regex:

    var pageNum = $("#specificLink").attr("href").match(/page=([0-9]+)/)[1];
    
    0 讨论(0)
提交回复
热议问题