我在試圖找到一個有哈希值(#)的專案時遇到了麻煩,我想把url中的哈希值改為另一個識別符號。例如,我有一個像這樣的url => http://localhost:8088/stores/1/brands?q=#當用哈希值搜索時,url將改變為像這樣的=> http://localhost:8088/stores/1/brands?q=#
如何像這樣替換并將其推送到當前的URL?
。let currentUrl = 'http://localhost:8088/stores/1/brands?q=#'/span>;
let url = new URL(currentUrl)。
url.searchParams.set("q", "#"); //設定引數.
url.hash=''。
let newUrl = url.href;
console.log(newUrl);
<script src="https://cdnjs. cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
uj5u.com熱心網友回復:
你可以使用函式encodeURIComponent。
例如:
encodeURIComponent("#"/span>) //給出#
你可以在MDN docs上閱讀詳細內容。https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent
uj5u.com熱心網友回復:
你可以使用replace
。let currentUrl = "http://localhost:8088/stores/1/brands?q=#"/span>;
str = currentUrl.replace("#"/span>, "023"/span>)。
console.log(str)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/331836.html
標籤:
