【问题标题】:How to load a different text when i click on element at the same page?当我单击同一页面上的元素时如何加载不同的文本?
【发布时间】:2018-02-20 13:26:23
【问题描述】:

我目前正在尝试构建一个网站,该网站将在同一页面中执行我想要的所有功能,而不是在不同页面中导航。我的页面现在是这样的

当我从侧边栏单击客户端时,我希望加载一个表单来代替我在侧边栏右侧的欢迎用户消息。所有客户的形式都是相同的。是否也可以将我将选择的客户端保存在一个变量中,以便我以后可以使用它来接收来自 mysql 数据库的元素?

有人可以请教我吗? 我的源代码如下:

<!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> 

在此致谢

【问题讨论】:

  • 我会给你大致的想法。将带有欢迎消息的整个 html 放在一个 div 中。并将该 div 设置为 .active 类。当您单击列表中的一项时,请执行 $(".active").removeClass("active")。并将 .active 类添加到您的 li 所链接的内容中。默认情况下,这些 div 是 display:none,默认情况下你有一个 .active 。 .active 是 display:block。多田。
  • 这对于 SO 的 QA 格式 IMO 来说太宽泛了...

标签: javascript php angularjs


【解决方案1】:

据我了解,您正在尝试开发 SPA(单页应用程序)。

我建议您使用框架/库,如 AngularJS 或 ReactJS,并使用那里的路由功能来实现这一点。

但是如果你是 JS 的初学者。

您可以通过使用Jquery's onClick().html() 函数来做到这一点:

请参考此链接:

Replace Div Content onclick

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-06
    • 1970-01-01
    • 2011-12-01
    • 2014-02-20
    相关资源
    最近更新 更多