我有第 1 頁的網址為 http://www.dev-crn.com:5703/tag/Cloud 點擊按鈕接下來我需要將第 2 頁的網址更新為 http://www.dev-crn.com: 5703/tag/Cloud/1 我如何在 javascript ot jquery 中做到這一點?我已經有一個引數宣告為 var pagenumber 和 var urlPath = window.location.pathname; 獲取鏈接的位置。如何使其動態增加網址?
在我的情況下,頁碼是總頁數,我有 8 頁,所以每次單擊下一步按鈕時,我都需要將 url 增加到 8 頁
uj5u.com熱心網友回復:
window.location = `${window.location.host}/tag/Cloud/${pagenumber}`;
uj5u.com熱心網友回復:
你能試試這個:
您的所有頁面都將擁有這三個按鈕。
只需更改每個頁面的中間按鈕的值。
希望我理解你的要求。
$('.change_page').click(function() {
var this_page = $('#this_page').html();
var new_location = '';
var this_location = window.location.href;
var this_action = $(this).attr('id');
if (this_action == 'previous' && this_page != '1') {
var page_number = parseInt(this_page) - 1;
new_location = this_location '/' page_number;
}
if (this_action == 'next' && this_page != '8') {
var page_number = parseInt(this_page) 1;
new_location = this_location '/' page_number;
}
console.log(new_location);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
This page is currently on page number 4<br/>
<button id="previous" class="change_page">previous</button>
<button id="this_page">4</button>
<button id="next" class="change_page">next</button>
uj5u.com熱心網友回復:
您不能“增加”一個 URL,但您可以增加數字并使用它們來構建您的 URL。
const BASE_URL = "https://example.com";
const MAX_POSITIONS = 8;
const BTN_NEXT = document.getElementById("btn-next");
const BTN_PREV = document.getElementById("btn-prev");
const URL_VALUE = document.getElementById("url-value");
const _createUrl = function(position) {
if (position > 0) {
return BASE_URL "/" position;
}
return BASE_URL;
};
let currentPosition = 0;
BTN_NEXT.addEventListener("click", function() {
if (currentPosition === MAX_POSITIONS - 1) {
return;
}
currentPosition ;
URL_VALUE.textContent = _createUrl(currentPosition);
});
BTN_PREV.addEventListener("click", function() {
if (currentPosition === 0) {
return;
}
currentPosition--;
URL_VALUE.textContent = _createUrl(currentPosition);
});
<button id="btn-next">Next</button>
<button id="btn-prev">Prev</button>
<div>
<h4>URL</h4>
<span id="url-value"></span>
</div>
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/475387.html
標籤:javascript html jQuery
上一篇:jQuery檢查表中的所有復選框
