简体   繁体   中英

How to open tabs from sidebar menu?

I am trying to create a web application that will send messages to different users. I know how to send the messages and all but i stack somewhere and i want some guidance. At the moment my web application looks like this: 在此处输入图片说明

What i want to do is when i click on any of the clients listed in the drop down menu to change the main panel and dispayy the client name instead of the welcome text.

For example when i click on client 1 i want the text on the right("Welcome user......) to be replaced with just Client 1

My source Code is the following:

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<style>
body {
    font-family: "Lato", sans-serif;
}

/* Fixed sidenav, full height */
.sidenav {
    height: 100%;
    width: 200px;
    position: fixed;
    z-index: 1;
    top: 0;
    left: 0;
    background-color: #111;
    overflow-x: hidden;
    padding-top: 20px;
}

/* Style the sidenav links and the dropdown button */
.sidenav a, .dropdown-btn {
    padding: 6px 8px 6px 16px;
    text-decoration: none;
    font-size: 20px;
    color: #818181;
    display: block;
    border: none;
    background: none;
    width: 100%;
    text-align: left;
    cursor: pointer;
    outline: none;
}

/* On mouse-over */
.sidenav a:hover, .dropdown-btn:hover {
    color: #f1f1f1;
}

/* Main content */
.main {
    margin-left: 200px; /* Same as the width of the sidenav */
    font-size: 20px; /* Increased text to enable scrolling */
    padding: 0px 10px;
}

/* Add an active class to the active dropdown button */
.active {
    background-color: green;
    color: white;
}

/* Dropdown container (hidden by default). Optional: add a lighter background color and some left padding to change the design of the dropdown content */
.dropdown-container {
    display: none;
    background-color: #262626;
    padding-left: 8px;
}

/* Optional: Style the caret down icon */
.fa-caret-down {
    float: right;
    padding-right: 8px;
}

/* Some media queries for responsiveness */
@media screen and (max-height: 450px) {
    .sidenav {padding-top: 15px;}
    .sidenav a {font-size: 18px;}
}
</style>
</head>
<body>

<div class="sidenav">
  <button class="dropdown-btn">Clients 
    <i class="fa fa-caret-down"></i>
  </button>
  <div class="dropdown-container">
    <a href="#">Client 1</a>
    <a href="#">Client 2</a>
    <a href="#">Client 3</a>
  </div>
</div>

<div class="main">
  <h2>Welcome user</h2>
  <p>Click on the dropdown button to open the dropdown menu inside the side navigation.</p>
  <p>This sidebar is of full height (100%) and always shown.</p>
  <p>Some random text..</p>
</div>

<script>
/* Loop through all dropdown buttons to toggle between hiding and showing its dropdown content - This allows the user to have multiple dropdowns without any conflict */
var dropdown = document.getElementsByClassName("dropdown-btn");
var i;

for (i = 0; i < dropdown.length; i++) {
  dropdown[i].addEventListener("click", function() {
    this.classList.toggle("active");
    var dropdownContent = this.nextElementSibling;
    if (dropdownContent.style.display === "block") {
      dropdownContent.style.display = "none";
    } else {
      dropdownContent.style.display = "block";
    }
  });
}
</script>

</body>
</html> 

How can i do that? Thanks in Regards

如果您单击侧面菜单中的链接,还是停留在同一页面上,将会加载新页面吗?

Fiddle version: https://jsfiddle.net/wr1t3r/vj81c2wv/

Explanation:

If you want to use pure JS it's possible to do it this way:

  1. Add onclick attribute to all of your client "a":

    <a href="#" onclick="document.getElementById('client-name').innerHTML = this.innerHTML"> Client 1 </a>

  2. Add ID to your H2 for event:

    <h2 id="client-name">Welcome user</h2>

Same can be done with nicer way by creating javascript function:

  1. Add onclick event to all of your client "a":

    <a href="#" onclick="setClient(this.innerHTML)"> Client 1 </a>

  2. Add ID to your H2 for easier DOM manipulation:

    <h2 id="client-name">Welcome user</h2>

  3. Create function in your javascript code:

    function setClient(client_name) { document.getElementById('client-name').innerHTML = client_name; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM