jquery - Finding a class by dynamic id and accessing the data value
问题描述
I have tree structured data where I want to get the id's of each parent on clicking on particular parent. This is similar to my question here
I am unable to get the required alert
can some one let me know what is wrong
$(".chapter").on("click", function() {
let id = $(this).attr("href");
list = $.map($(id).find(".sportlist"), function(item) {
alert($(item).data("value"))
})
});
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.css" rel="stylesheet" />
<div class="tree-ehex col-md-3" style="padding-left:0px;float: left; width:215px;overflow-x:scroll;height:261px;" id="treeOrderDiv">
<ul style="padding-left: 0px;" id="leftsubMenus-Ul">
<li class="parent_li">
<span style="font-family: serif;font-size: small;" title="Collapse this branch"><i class="tree-ehex-title glyphicon glyphicon-minus-sign"></i></span>
<a href="#Parent" class='chapter'> Parent</a>
<ul id='#Parent'>
<li style="display: list-item;" data-value='20'>
<a href="#"><span style="text-transform: uppercase;color:black;font-size: smaller;font-family: sans-serif"> Child1 </span></a>
</li>
<li style="display: list-item;" data-value='22'>
<a href="#"><span style="text-transform: uppercase;color:black;font-size: smaller;font-family: sans-serif"> Child2 </span></a>
</li>
</ul>
</li>
<li class="parent_li">
<span style="font-family: serif;font-size: small;" title="Collapse this branch"><i class="tree-ehex-title glyphicon glyphicon-minus-sign"></i></span>
<a href="#Parent1" class='chapter'> Parent1</a>
<ul id="#Parent1">
<li class='sportlist' style="display: list-item;" data-value='22'>
<a href="#"><span style="text-transform: uppercase;color:black;font-size: smaller;font-family: sans-serif"> Child</span></a>
</li>
<li class='sportlist' style="display: list-item;" data-value='24'>
<a href="#"><span style="text-transform: uppercase;color:black;font-size: smaller;font-family: sans-serif"> Child1 </span></a>
</li>
</ul>
</li>
</ul>
</div>
解决方案
You have number of options:
- change
id
attrs oful
from<ul id="#Parent">
to<ul id="Parent">
and leave js as-is - or leave html as-is, but change js:
$(".chapter").on("click", function() {
let id = $(this).attr("href");
let chapter = $(document.getElementById(id));
list = $.map(chapter.find(".sportlist"), function(item) {
alert($(item).data("value"))
})
});
推荐阅读
- visual-studio - Visual Studio 2015 社区版在登录时总是崩溃
- json - Ionic 4 使用搜索栏读取本地 JSON 文件
- python - 在数据框中搜索多个子值
- postgresql - PostgreSQL 使用 Linux 大页面
- android - 我的 var 值不会在第一次“点击”时更新
- java - 如何使 Failsafe RetryPolicy 处理 ConstraintVoilationException
- python - 如何在python中删除图像的背景
- reactjs - ReferenceError:找不到变量:React 上的 regeneratorRuntime
- dspace - 在 DSpace 实例中集成文档可视化器
- kubernetes - 如何将 pod 内的文件复制到外部?