Change HREF of a link if url does not meet some requirements

3

I have a question. I need to force the page 1 value of a page to a specific URL. I could not figure out how to copy the code of the plugin I'm using, so I decided to make a hint so I could put the system into production. I would like, via JQUERY, to change the HREF attribute of a certain "A" tag, if its class is "page smaller" and if the url defined does not have the "page" string. But I'm getting caught up in JS. I tried the following:

    $(function(){
                if(!$('a[href*="page"]') && $('a').hasclass('page smaller')){    
    $("a").attr("href", "http://10.31.0.137/wordpress/index.php/ajax-fonte/page/0")
                }
});

In summary, I need to force the link pointing to link , point to link

Here is the paging code to illustrate

<div class="wp-pagenavi">
<span class="pages">Página 3 de 44</span>
<a class="previouspostslink" rel="prev" href="http://10.31.0.137/wordpress/index.php/ajax-fonte/page/2/">«</a>
<a class="page smaller" href="http://10.31.0.137/wordpress/index.php/ajax-fonte/">1</a>
<a class="page smaller" href="http://10.31.0.137/wordpress/index.php/ajax-fonte/page/2/">2</a>
<span class="current">3</span>
<a class="page larger" href="http://10.31.0.137/wordpress/index.php/ajax-fonte/page/4/">4</a>
<a class="page larger" href="http://10.31.0.137/wordpress/index.php/ajax-fonte/page/5/">5</a>
<span class="extend">...</span>
<a class="larger page" href="http://10.31.0.137/wordpress/index.php/ajax-fonte/page/10/">10</a>
<a class="larger page" href="http://10.31.0.137/wordpress/index.php/ajax-fonte/page/20/">20</a><a class="larger page" href="http://10.31.0.137/wordpress/index.php/ajax-fonte/page/30/">30</a>
<span class="extend">...</span>
<a class="nextpostslink" rel="next" href="http://10.31.0.137/wordpress/index.php/ajax-fonte/page/4/">»</a>
</div>
    
asked by anonymous 20.06.2016 / 02:39

1 answer

3

You can get the element with .page.smaller:not([href*=page]) .

This will bring all elements that have the page and smaller classes in which the href attribute does not contain the "page" string:

$(function(){

  $('.page.smaller:not([href*=page])')
     .attr('href', 'http://10.31.0.137/wordpress/index.php/ajax-fonte/page/0');

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><divclass="wp-pagenavi">
  <!-- ... -->
  <a class="page smaller" href="http://10.31.0.137/wordpress/index.php/ajax-fonte/">1</a>
  <a class="page smaller" href="http://10.31.0.137/wordpress/index.php/ajax-fonte/page/2/">2</a>
  <!-- ... -->
</div>

As an alternative suggestion, since you are only wanting to concatenate the page/0 in the href already defined in the element, instead of pasting the absolute URL, you can do this:

$(function(){

  $('.page.smaller:not([href*=page])')
     .attr('href', function(i, href){
        return href + 'page/0';
     });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><divclass="wp-pagenavi">
  <!-- ... -->
  <a class="page smaller" href="http://10.31.0.137/wordpress/index.php/ajax-fonte/">1</a>
  <a class="page smaller" href="http://10.31.0.137/wordpress/index.php/ajax-fonte/page/2/">2</a>
  <!-- ... -->
</div>
    
20.06.2016 / 03:14