2017-01-24 10 views
0

JavaScriptのダイナミックアンカーhrefを変更するコードに取り組んでいます。私は.replace()に完全にhrefを提案する投稿を見ました。私は以下のようにパスに余分にpageを追加する方法を探していました。javascriptでアンカーhrefを部分的に変更する最も良い方法は何ですか

元のURL: http://localhost:8000/2

変化に: http://localhost:8000/page/2

HTMLマークアップ(laravel):

{% if posts.lastPage > 1 %} 
<ul class="pagination"> 
    {% if posts.currentPage > 1 %} 
    <li> 
     <a href="{{ this.page.baseFileName|page({ (pageParam): (posts.currentPage-1) }) }}"aria-label="Previous"> 
      <span aria-hidden="true">&laquo;</span> 
     </a></li> 
    {% endif %} 

    {% for page in 1..posts.lastPage %} 
    <li class="{{ posts.currentPage == page ? 'active' : null }}"> 
     <a href="{{ this.page.baseFileName|page({ (pageParam): page }) }}">{{ page }}</a> 
    </li> 
    {% endfor %} 

    {% if posts.lastPage > posts.currentPage %} 
    <li><a href="{{ this.page.baseFileName|page({ (pageParam): (posts.currentPage+1) }) }}" aria-label="Next"> 
     <span aria-hidden="true">&raquo;</span> 
    </a> 
    </li> 
    {% endif %} 
</ul> 
{% endif %} 

Javascriptを:

$('.pagination').on('click', 'a', function(event) { 
    event.preventDefault(); 
    var postUrl = $(this).attr("href"); --> need to modify href path 
    var post = "default "; 
    post= $.ajax({type: "GET", url: postUrl, async: false}).responseText; 
    document.getElementById("container").innerHTML = post; 
}); 

**注:**私はwindow.location.hrefで使用regexsplit()の使用例を見てきました。 hrefregexまたはsplit()を使用できますか?

ありがとうございました。

+1

なぜあなたはスプリットの正規表現を使用することはできないでしょうか?それは単なる文字列です。スライス(0、-1).join( '/')+ '/ page /' + url.split( '/')。pop() 'または何か? – somethinghere

答えて

1

hrefプロパティを変更するだけの理由はありますか?

var postUrl = $(this).attr("href"); //get 
$(this).attr("href",newUrl); //set 

新しいURLを作成するには、regexpを使用できますが、URL構造によって異なります。あなたの例に基づいて、あなたのような何かができる:

var newUrl = postUrl.replace(/((http|https):\/\/.+?\/)(.)/,"$1page/$3"); 

説明:交換

$1      Inserts the first group, the first () 
page/     Followed by the string "page/" 
$3      Followed the third group 

で次に

/      Starts the regexp (note that this is not a string and it's delimited by /--it could be a string, too, tho) 
    (     Starts a new group, so we can use the match in the replacement 
     (http|https)  Matches the strings http or https (just in case if the protocol may vary). It is also a group as is between brackets 
     :\/\/   Must be followed by :// (the/are escaped with \) 
     .    Can be followed by any character 
     +?    This means that the previous element (the dot) must appear at least once and the ? makes it not greedy, matching the minimum number of time (otherwise it will match everything until the end of the string) 
     \/    Followed by/(again, escaped) 
    )     End the group 
    (     Starts new group to match what is after http://localhost.../ 
     .    Matches anything 
    )     Ends the group 
/      End the expression 

第二のグループは、httpれる| httpsのは、私たちは「ドンすでに最初のグループに入っているので、必要としません。

+0

あなたが良いものを知っていれば 'regex'の文書へのリンクを提供できるかどうか分かります – Poorya

+1

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExpこの例の説明を追加します。 – Gabriel

0

ただ、例えばreplaceを使用します。

var href = $(this).attr("href"); 
href.replace(/(\/[0-9]+?)$/g, "/page$1"); 
関連する問題