Jquery - 如何在链接中更改属性href的一部分?

use*_*042 2 javascript jquery

我不知道在从?page到&page的链接中更改href属性我做错了什么.它留在?页面上.谢谢你的任何建议.

jQuery的:

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

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>
Run Code Online (Sandbox Code Playgroud)

eli*_*ide 5

您需要实际设置属性:

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

请注意,我添加了.之前pagination使用attr('href')而不仅仅是使用href.

这是一个有效的jsFiddle.


Dar*_*jax 5

如果您不介意更多地更改代码,可以尝试这种更简单的方法:

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

你真的不需要.each()也不需要替换?page,&page除非你有额外?的hrefs ...

示例JSFiddle