简体   繁体   English

如何使用 css 和 html 在单击垂直侧边栏菜单时显示子列表?

[英]How to show sub list on click of vertical sidebar menu using css and html?

I have a sidebar in which there are href's and on click of particular href, I want to show the list of data.我有一个侧边栏,其中有 href,点击特定的 href,我想显示数据列表。

I have used css with html to get the desired results, but when i click on href, i want to append the buttons to that href only but it is appending the buttons to the bottom of side bar.我已经使用 css 和 html 来获得所需的结果,但是当我单击 href 时,我想要 append 按钮,但它只是将按钮附加到底部侧栏。

<script 
src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"> 
</script>
<div class="sidenav">
<a href="#" onclick="javascript:myFunc()">col1</a>
<a href="#">col2</a>
<script> function myFunc() {

var $container = $("#container");
var $sidebar = $("#sidebar");
var button = $('<button />');
var item= 'abc';
button.text(item);
$sidebar.append(button);
var data='cde';
var button = $('<button />');
button.text(data);
$sidebar.append(button);
}</script>

</div>
<div id="container">
    <div id="sidebar">
    </div>
</div>

here is the link to what i have tried- https://jsfiddle.net/gaurav10022/exrLaz14/1/ I want that buttons gets appended after col1 not col2 and also when i click on col1 again it toggles.这是我尝试过的链接- https://jsfiddle.net/gaurav10022/exrLaz14/1/我希望在col1而不是col2之后附加按钮,并且当我再次单击col1时它会切换。

I am not sure you are looking exactly what, however, You can try with below approach.我不确定您正在寻找什么,但是,您可以尝试以下方法。 just hide the div during page load and show and hide based on click event.只需在页面加载期间隐藏 div 并根据点击事件显示和隐藏。

 function myFunc() { var $container = $("#container"); var $sidebar = $("#sidebar"); if($sidebar.children().length <= 0) { var button = $('<button />'); var item= 'abc'; button.text(item); $sidebar.append(button); var data='cde'; var button = $('<button />'); button.text(data); $sidebar.append(button); $('#col1').append($container); } else { $sidebar.html(''); } }
 .sidenav { width: 100px; position: relative; z-index: 1; top: 101; left: 201; background: #FFF; overflow-x: hidden; padding: 8px 0; }.sidenav a { padding: 6px 8px 6px 16px; text-decoration: none; font-size: 25px; color: #818181; display: block; }.sidenav a:hover { color: #064579; } button { display: block; cursor: pointer; background-color: #FFF; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"> </script> <div class="sidenav"> <a href="#" onclick="myFunc()" id= "col1">col1</a> <a href="#">col2</a> </div> <div id="container" style="dsiplay: none"> <div id="sidebar"> </div> </div>

Please don't hesitate to let me know if you have any further query.如果您有任何进一步的疑问,请随时告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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