我正在尝试创建一个将消息发送给不同用户的Web应用程序。我知道如何发送消息,除了我堆叠在某处,我想要一些指导。目前,我的Web应用程序如下所示:
我想要做的是当我点击下拉菜单中列出的任何客户端来更改主面板并发送客户名称而不是欢迎文本。
例如,当我点击客户端1时,我希望右侧的文本("欢迎用户......)仅用客户端1替换
我的源代码如下:
<!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>
我该怎么做? 谢谢你的问候
答案 0 :(得分:0)
如果您点击侧边菜单中的链接或者您是否在同一页面上,是否会加载新页面?
答案 1 :(得分:0)
小提琴版:https://jsfiddle.net/wr1t3r/vj81c2wv/
<强>解释强>
如果你想使用纯JS,可以这样做:
将onclick属性添加到您的所有客户端“a”:
<a href="#" onclick="document.getElementById('client-name').innerHTML = this.innerHTML">
Client 1
</a>
将事件ID添加到您的H2:
<h2 id="client-name">Welcome user</h2>
通过创建javascript函数,可以通过更好的方式完成同样的事情:
将onclick事件添加到您的所有客户端“a”:
<a href="#" onclick="setClient(this.innerHTML)">
Client 1
</a>
为H2添加ID以便更轻松地操作DOM:
<h2 id="client-name">Welcome user</h2>
在您的javascript代码中创建函数:
function setClient(client_name) {
document.getElementById('client-name').innerHTML = client_name;
}