如何从click事件对象得到一个链接的href属性的参数属性、对象、参数、事件

2023-09-10 16:45:47 作者:16.梨花雨凉

有没有一种简单的方法使用click事件对象来获取参数,在点击链接的href属性的结束?

Is there a simple way to get the parameters at the end of an href attribute of a clicked link using the click event object?

我有一些jQuery code,它是这样的:

I have some jQuery code that looks like this:

$(document).ready(function() {
    $('#pages').delegate("a", "click", function(e) {
        var formData = "parameters to clicked link";
        $.ajax({
            url: 'friends2.php',
            dataType: 'json',
            data: formData,
            success: function(data) {
                $('#searchbutton').attr("disabled", false);
                $('#searchresults').html(data.results);
                $('#pages').html(data.paginate);
            }
        });//ajax end
        return false;
    });
});

和这里是有关HTML:

And here is the relevant HTML:

<div id="pages">
<span class="disabled">previous</span>
<span class="current">1</span>
<a href="friends.php?term=ma&p=2">2</a>
</div>

我想要做的就是分页,我已经与阿贾克斯做了搜索的结果。搜索正常运行和获得与查询匹配的用户列表。创建分页链接的结果脚本的一部分还在。

What I'm trying to do is paginate the results of a search that I've done with ajax. The search runs fine and a get a list of users that match the query. The part of the results script that creates the pagination links is also working.

在上面的例子中,有两页的结果。我想,当用户点击链接去结果第2页做的是拦截链接点击,而是创建一个使用期限= MA&放一个新的Ajax请求; P = 2 作为数据传递到请求

In the example above, there are two pages of results. What I want to do when a user clicks the link to go to page 2 of the results is to intercept the link click and instead create a new ajax request using term=ma&p=2 as the data passed to the request.

所以,长话短说,有一个简单的方法来获得期限= MA&安培; P = 2 从事件对象通过匿名函数在我的jQuery的上面?

So, long story short, is there an easy way to get term=ma&p=2 from the event object passed the the anonymous function in my jQuery above?

推荐答案

您可以使用 this.href 方法读取链接属性:

You can use the this.href method to read the link attribute:

$('#pages').delegate("a", "click", function(e) {
   var str = this.href.split('?')[1];

例如:

str = 'friends.php?term=ma&p=2';
console.log(str.split('?')[1]); // output: term=ma&p=2