如果 url 包含“TicketPreDateable”一詞,我正在嘗試創建一個更改按鈕重定向鏈接的函式。所以我有一個這樣的按鈕:
<div class="col-12 col-md-6 col-xl-4" id="myproductid">
<figure class="effect-product mx-auto"><img class="figure-img img-fluid" src="myimage">
<figcaption><span class="badge badge-danger" style="top: -2.7em; background-color: red;">OFFRE SPéCIALE</span>
<h2 class="mt-0" style="color: white !important;"><span>My </span>Product Title</h2>
<p>Profitez des pistes le samedi lorsque d'autres sont occupés à faire leurs valises ou coincés dans les bouchons. Un tarif unique à 24€ vous donne accès en toute liberté au domaine skiable pour la journée.</p>
<a onclick="produrlmobile("https://www.mylink.com/fr/ProductsNgTicket/ticketPreDateable?poolNr=13&projNr=495&ticketTypeNr=122&preDatable=True&groupId=1&Day=11&Month=12&Year=2021")" href=""></a>
</figcaption>
</figure>
</div>
我的功能是這樣的:
function produrlmobile(produrl) {
if (produrl.includes("TicketPreDateable")) {
if (window.innerWidth < 960) {
window.location.href = produrl.replace(
"TicketPreDateable",
"TicketPreDateableMobile"
);
} else {
window.location.href = produrl;
}
} else {
window.location.href = produrl;
}
}
我的印象是該功能不起作用是因為 url 中的特殊字符,但我不知道如何避免這種情況。
感謝您的幫助,祝您有美好的一天!
uj5u.com熱心網友回復:
有關 HTML 問題,請參閱 Sarens 評論。
另請注意,該replace方法區分大小寫。在您的函式中,您似乎正在嘗試替換字串TicketPreDateable,但 url 僅包含ticketPreDateable. 我建議檢查您是否正確處理了替換方法引數中的舊字串和新字串的情況。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/383752.html
標籤:javascript 功能 包括
下一篇:如何使用反應式物件創建函式?
