首页 > 解决方案 > 如何重定向到另一个页面并在该页面上触发事件

问题描述

单击此按钮时,我创建了一个带有 id="work" 的按钮我试图重定向到联系我们页面,并且在联系我们页面中单击了带有 id="working-us" 的按钮。

jQuery

$("#work").click(function() {
    window.location.replace("contactus.html");
    sessionStorage.setItem("workWithUS", "true");
    if (sessionStorage.workWithUs == "true") {
        $("#working-us").trigger(click);
        sessionStorage.removeItem("workWithUs");
    });

HTML

<a href="javascript:void(0);" id="work">Join our Team</a><button class="topic-btn d-flex" data-toggle="tab" href="#working-us" id="work2">

标签: jqueryhtml

解决方案


您的代码看起来不错,但缺少一个右括号,这可能会阻止 if 语句触发:

$("#work").click(function() {
  window.location.replace("contactus.html");
  sessionStorage.setItem("workWithUS", "true");
  if (sessionStorage.workWithUs == "true") {
    $("#working-us").trigger(click);
    sessionStorage.removeItem("workWithUs");
  }
});

推荐阅读