【发布时间】:2022-01-19 08:35:04
【问题描述】:
如果 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中的特殊字符导致该功能不起作用,但我不知道如何避免。
感谢您的帮助,祝您有愉快的一天!
【问题讨论】:
-
它不起作用,因为它没有薪水……不开玩笑,更明确地说明您遇到的错误或面临的问题。我认为函数 produrlmobile 也作为字符串,所以你应该以这种方式发送 url
produrlmobile('https://www.mylink.com/fr/ProductsNgTicket/ticketPreDateable?poolNr=13&projNr=495&ticketTypeNr=122&preDatable=True&groupId=1&Day=11&Month=12&Year=2021)'我添加了简单的 qote -
该功能与简单的引用完美配合,非常感谢您的回答,下次我会更加清晰明确
标签: javascript function include