【问题标题】:JS redirect functionJS重定向功能
【发布时间】: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&amp;projNr=495&amp;ticketTypeNr=122&amp;preDatable=True&amp;groupId=1&amp;Day=11&amp;Month=12&amp;Year=2021)' 我添加了简单的 qote
  • 该功能与简单的引用完美配合,非常感谢您的回答,下次我会更加清晰明确

标签: javascript function include


【解决方案1】:

有关 HTML 的问题,请参阅 Sarens 评论。

另外,请注意replace 方法区分大小写。在您的函数中,您似乎正在尝试替换字符串TicketPreDateable,但网址仅包含ticketPreDateable。 我建议检查您是否正确处理了替换方法参数中的旧字符串和新字符串。

【讨论】:

  • 确实,我没有看到这个错误:我必须更仔细地重新阅读您和Sharen的解决方案,一切正常,非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-20
  • 2011-07-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多