admin管理员组

文章数量:1325108

I don't know what I am doing wrong with changing href attribute in link from ?page to &page. It stays on ?page. Thank you for any advice.

Jquery:

var article_pagination_change = function(){
    $('pagination a').each(function(){
       $(this).href.replace('?page','&page');
    });
}
article_pagination_change();

HTML:

<div id="pagination80" class="pagination" style="">
  <a class="first" data-action="first" href="?page=1">&lt;&lt;</a>
  <a class="previous" data-action="previous" href="?page=1">&lt;</a>
  <input class="pag_input_80" type="text" value="1 of 12" data-max-page="12" readonly="readonly">
  <a class="next" data-action="next" href="?page=2">&gt;</a>
  <a class="last" data-action="last" href="?page=12">&gt;&gt;</a>
</div>

I don't know what I am doing wrong with changing href attribute in link from ?page to &page. It stays on ?page. Thank you for any advice.

Jquery:

var article_pagination_change = function(){
    $('pagination a').each(function(){
       $(this).href.replace('?page','&page');
    });
}
article_pagination_change();

HTML:

<div id="pagination80" class="pagination" style="">
  <a class="first" data-action="first" href="?page=1">&lt;&lt;</a>
  <a class="previous" data-action="previous" href="?page=1">&lt;</a>
  <input class="pag_input_80" type="text" value="1 of 12" data-max-page="12" readonly="readonly">
  <a class="next" data-action="next" href="?page=2">&gt;</a>
  <a class="last" data-action="last" href="?page=12">&gt;&gt;</a>
</div>
Share Improve this question asked Mar 28, 2014 at 22:57 user3342042user3342042 2411 gold badge7 silver badges20 bronze badges 1
  • that dot is very small I can't see it :) thank you – user3342042 Commented Mar 28, 2014 at 23:01
Add a ment  | 

2 Answers 2

Reset to default 5

You need to actually set the attribute:

var article_pagination_change = function(){
    $('.pagination a').each(function(){
        var newurl = $(this).attr('href').replace('?page','&page');
        $(this).attr('href', newurl);
    });
}
article_pagination_change();

Note that I added the . before pagination and am using attr('href') instead of just href.

Here's a working jsFiddle.

If you don't mind changing your code a bit more, you can try this simpler approach:

var article_pagination_change = function(){
    $('.pagination a').attr('href',function(index,attr){
       return attr.replace('?','&');
    });
}
article_pagination_change();

You don't really need the .each() nor replacing ?page with &page unless you had extra ?s on your hrefs...

Sample JSFiddle

本文标签: javascriptJqueryHow to change part of attribute href in linkStack Overflow