JQuery如何从href标签中提取值?

时间:2022-11-27 17:51:09

I am new to JQuery.

我是JQuery的新手。

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

如果我有以下标签。从href中提取“page”值的最佳JQuery方法是什么。

<a href="Search/Advanced?page=2">2</a>

Malcolm

马尔科姆

6 个解决方案

#1


64  

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

我想到的第一件事就是单行正则表达式:

var pageNum = $("#specificLink").attr("href").match(/page=([0-9]+)/)[1];

#2


16  

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

我不知道你是否会使用它进行分页并在 -tag中显示文本,但如果你应该也可以

var number = $('a').text();

#3


6  

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

然后看一下这个插件:http://plugins.jquery.com/project/query-object

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

它将帮助您处理您想要做的各种查询事务。

/Peter F

/彼得F.

#4


4  

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

这是一个通过将查询字符串转换为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... =)

我会像其他人建议的那样去图书馆... =)

#5


0  

Use this jQuery extension by James Padoley

使用James Padoley的这个jQuery扩展

#6


-1  

if ($('a').on('Clicked').text().search('1') == -1)
{
    //Page == 1
}
else
{
    //Page != 1
}

#1


64  

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

我想到的第一件事就是单行正则表达式:

var pageNum = $("#specificLink").attr("href").match(/page=([0-9]+)/)[1];

#2


16  

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

我不知道你是否会使用它进行分页并在 -tag中显示文本,但如果你应该也可以

var number = $('a').text();

#3


6  

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

然后看一下这个插件:http://plugins.jquery.com/project/query-object

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

它将帮助您处理您想要做的各种查询事务。

/Peter F

/彼得F.

#4


4  

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

这是一个通过将查询字符串转换为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... =)

我会像其他人建议的那样去图书馆... =)

#5


0  

Use this jQuery extension by James Padoley

使用James Padoley的这个jQuery扩展

#6


-1  

if ($('a').on('Clicked').text().search('1') == -1)
{
    //Page == 1
}
else
{
    //Page != 1
}