[英]Creating a Tree Style-List with JQuery
I have a javascript function that creates <ul>
elements for each index in an array that is created from a MySqli query done in PHP.我有一个 javascript 函数,它为数组中的每个索引创建
<ul>
元素,该数组是从在 PHP 中完成的 MySqli 查询创建的。
What I want to do is allow a user to click each <ul>
element to reveal their child <ul>
(which can also be used to reveal children) or <li>
elements (which can be clicked to call a function).我想要做的是允许用户单击每个
<ul>
元素以显示其子元素<ul>
(也可用于显示子元素)或<li>
元素(可单击以调用函数)。
HTML, JS, PHP:
<div id ="fileTreeContainer"></div>
<script type="text/javascript">
<?php
$genre_query = "SELECT DISTINCT genre FROM songs ORDER BY genre ASC;";
$genre_result = $connect -> query($genre_query);
$genre_row = $genre_result -> fetch_array(MYSQLI_NUM);
$genre_array = json_encode($genre_row);
echo "var genreArray = " . $genre_array . ";";
?>
function genreList(item) {
$("#fileTreeContainer").append("<ul class=\"genre\">" + item + "<li class=\"hidden\">\"artist\">jfkjfk</li></ul>");
}
genreArray.forEach(genreList);
</script>
CSS:
.hidden { display: none; }
I've tried using a number of libraries designed to create tree-style menus like this but I haven't been able to get anything to work.我已经尝试使用许多旨在创建这样的树形菜单的库,但我一直无法得到任何工作。
I'm using $('.genre').click( function() { $('.hidden', this).toggle(); } );
我正在使用
$('.genre').click( function() { $('.hidden', this).toggle(); } );
to hide/reveal the child element but this does not allow for the child element to be clicked.隐藏/显示子元素,但这不允许单击子元素。
I also have React included but I'm still not familiar with it.我也包含 React,但我仍然不熟悉它。
Could someone help me out?有人可以帮我吗? I've been browsing existing questions and trying solutions all day to no avail.
我一整天都在浏览现有问题并尝试解决方案,但无济于事。
I ended up just using nested <details><summary
elements like this:我最终只使用了嵌套的
<details><summary
元素,如下所示:
<details><summary>summary1</summary>
<details><summary>summary2</summary>
<details><summary>summary3</summary>
<p>content1</p>
</details>
<p>content2</p>
<p>content3</p>
</details>
<details><summary>summary4</summary>
<p>content5</p>
<p>content6</p>
</details>
</details>
While still using jquery to create the elements.虽然仍然使用 jquery 来创建元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.