从子元素的标题中获取HTML元素

get an html element from the title of a child element

本文关键字:元素 获取 HTML 标题      更新时间:2023-09-26

我有一个列表,其中有一个活动的class元素

<ul class="tabs clearfix" >
<li>  
  <a title="abc" href=# >ABC</a> 
</li>
<li>
  <a title="xyx" href=# >XYZ</a>
</li>
<li class=active > 
  <a title="all" href=# >All</a> 
</li>

我使用本地存储来存储标签的标题,然后用于相应地加载内容。当我从本地存储取回标题时,我很难获得特定的li元素。我想添加一个活动类到li元素,对应于特定的标题。下面是javascript代码:

$(document).ready(function(){
var tabs = $('.tabs > li');
if (localStorage.getItem("tabIndex") === null) {
    $( "#content" ).load( "{{url_for('mypage', query=query)}}" );
}else{
    var lastIndex = localStorage.getItem('tabIndex');
    **Do something to get the specific li element for the title**
    **addclass('active') for the li**
    LoadContent(lastIndex);
} 
tabs.on("click", function(){
    tabs.removeClass('active');
    localStorage.setItem('tabIndex', $('a', this).attr('title'));
    $(this).addClass('active');
    var index = $(this).children('a')[0].title;
    LoadContent(index);
})
});

我试过$("a[title=lastIndex]").parent().addClass('active');,但这似乎不起作用。一如既往,我真的很感谢这个网站提供给我的所有帮助。由于

您可以使用$('a[title="'+ lastIndex +'"]')来获取所需的元素,

例如:说你的lastIndex = xyz然后选择器变成$('a[title="xyz"]')