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">«</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">»</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
で使用regex
とsplit()
の使用例を見てきました。 href
にregex
またはsplit()
を使用できますか?
ありがとうございました。
なぜあなたはスプリットの正規表現を使用することはできないでしょうか?それは単なる文字列です。スライス(0、-1).join( '/')+ '/ page /' + url.split( '/')。pop() 'または何か? – somethinghere