如何将页面重定向到另一个页面并在jQuery中的重定向页面上执行代码?

时间:2019-03-30 09:03:16

标签: jquery html

从菜单上单击香蕉时我的主页出现问题,它将重定向到我的产品页面。主页到产品页面重定向很好,我想要的是重定向到产品页面后,它将在jquery中执行我的代码,并仅显示从主页单击的内容。默认情况下,我的“产品”页面显示所有项目。

主页:

    <!DOCTYPE html>
    <html>

    <head>
      <title>Home Page</title>
      <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
      <link rel='stylesheet' href='css/style.css'>
    </head>

    <body>
      <div class='container'>
        <div class='navbar navbar-expand-sm navbar-dark bg-dark'>
          <div class='navbar-brand'>
            Fruits
          </div>
          <ul class='navbar-nav'>
            <li class='nav-item'>
              <button id='hpbanana' class='btn nav-link'>Banana</button>
            </li>
            <li class='nav-item'>
              <button id='hpblueberry' class='btn nav-link'>Blueberry</button>
            </li>
            <li class='nav-item'>
              <button id='hpbaybas' class='btn nav-link'>Bayabas</button>
            </li>
          </ul>
        </div>
      </div>

      <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
      <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
      <script src='js/myjquery.js'></script>
    </body>

    </html>

产品页:                  

    <head>
      <title>Product Page</title>
      <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
      <link rel='stylesheet' href='css/style.css'>
    </head>

    <body>
      <div class='container'>
        <h3>
          <a href='index.html'>Fruits</a>
        </h3>
        <div class="list-group">
          <ul class='navbar-nav'>
            <li class='nav-item'>
              <button id='ppbanana' class="list-group-item list-group-item-action">Banana Image</button>
            </li>
            <li class='nav-item'>
              <button id='ppblueberry' class="list-group-item list-group-item-action">Blueberry Image</button>
            </li>
            <li class='nav-item'>
              <button id='ppbayabas' class="list-group-item list-group-item-action">Bayabas Image</button>
            </li>
          </ul>
        </div>
      </div>

      <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
      <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
      <script src='js/myjquery.js'></script>
    </body>

    </html>



         .hide {
             display: none;
            }



                  $(document).ready(() => {
                     $('#hpbanana').on('click', () => {
                        window.location.replace("product.html");
                        cyPageNum = 1;
                        if (cyPageNum==1){
                        alert('Banana');//for debugging
                       $('#ppblueberry').addClass('hide');
                       $('#ppbayabas').addClass('hide');
                        }
                      });
                     });

0 个答案:

没有答案