Firstly, there is no value
attribute available on an li
element, and adding non-standard attributes will mean that your page is invalid.
(首先, li
元素上没有可用的value
属性,添加非标准属性将意味着您的页面无效。)
Instead, use data-*
attributes.(而是使用data-*
属性。)
You can then hook to the click
event of the a
element and get that data
attribute from its parent li
, like this:
(然后,您可以挂钩到a
元素的click
事件,并从其父li
获取该data
属性,如下所示:)
$('li a').click(function(e) { e.preventDefault(); var value = $(this).closest('li').data('value'); // = 9 console.log(value); // do something with the value here... });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <ul> <li data-sequence="1" title="Category 1" class="liEllipsis selSubCategories" data-value="9"> <a href="#"> <span class="viewIcons delFaceName _delete fl"></span> Category 1 </a> </li> <li data-sequence="1" title="Category 2" class="liEllipsis selSubCategories" data-value="10"> <a href="#"> <span class="viewIcons delFaceName _delete fl"></span> Category 2 </a> </li> </ul>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…