如何使按钮导航到不同的页面?

时间:2019-01-15 08:45:28

标签: javascript html ajax navigation materialize

我想有一个带有导航按钮栏的主页,每个按钮在我的代码中为我打开一个不同的页面,并有一个“返回”按钮以返回主页。 我不知道如何使页面的html代码在主页中看不到,并且当我单击按钮时它们会出现(我更喜欢使用向右滑动动画),而当我单击“返回”按钮时,它将向左滑动回到主画面 这是我的代码,例如:

nav.nav-center ul {
  text-align: center;
}

nav.nav-center ul li {
  display: inline;
  float: none;
}

nav.nav-center ul li a {
  display: inline-block;
}
<!DOCTYPE html>
<html>

<head>

  <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">

  <!-- Latest compiled and minified CSS -->
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css" integrity="sha384-PmY9l28YgO4JwMKbTvgaS7XNZJ30MK9FAZjjzXtlqyZCqBY6X6bXIkM++IkyinN+" crossorigin="anonymous">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

  <!-- Optional theme -->
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap-theme.min.css" integrity="sha384-jzngWsPS6op3fgRCDTESqrEJwRKck+CILhJVO5VvaAZCq8JYf8HsR/HPpBOOPZfR" crossorigin="anonymous">

  <!-- Latest compiled and minified JavaScript -->
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js" integrity="sha384-vhJnz1OVIdLktyixHY4Uk3OHEwdQqPppqYR8+5mjsauETgLOcEynD9oPHhhz18Nw" crossorigin="anonymous"></script>

  <!-- Compiled and minified CSS -->
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">

  <!-- Compiled and minified JavaScript -->
  <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>

  <meta name="viewport" content="width=device-width, initial scale=1">

</head>
<style>

</style>

<body>
  <nav class="nav-center" role="navigation">
    <div class="nav-wrapper container">
      <ul>
        <li><a class="waves-effect waves-light btn">Button1</a></li>
        <li><a class="waves-effect waves-light btn">Button2</a></li>
      </ul>
    </div>
  </nav>

  <h4>This is supposed to be shown only after clicking the button1</h4>
  <p>i want the button to navigate the page so this will be shown only after button 1 is clicked</p>

  <h4>This is supposed to be shown only after clicking the button2</h4>
  <p>i want the button to navigate the page so this will be shown only after button 2 is clicked</p>


</body>

</html>

我只想查看主页中的按钮,当我单击它们时,将显示不带按钮的新页面,并带有返回首页按钮,并具有应该根据每个按钮显示的文本。 我目前所拥有的是立即显示整个html代码,并且没有对页面的划分

1 个答案:

答案 0 :(得分:1)

您必须创建单独的HTML文件才能在不同的路线上显示。如果您是初学者,我已经在此链接上更改了代码

Online Code

编辑:解释代码

为button1 button2创建两个文件(例如a.html b.html),并相应地粘贴其内容。

并在index.html中,删除已复制到a.html和b.html的代码。

在index.html中-

<a href="a.html" class="waves-effect waves-light btn">Button1</a>
<a href="b.html" class="waves-effect waves-light btn">Button2</a>

它应该可以工作。