我想检查当前网址是否包含父网址,以向父列表项链接添加“活动”类。
jQuery(function($) {
var path = window.location.pathname;
$('ul a').each(function() {
if (this.pathname.indexOf( path )) {
$(this).addClass('active');
}
});
});
我的html看起来像这样:
<ul>
<li><a class="list-link">Posts</a></li>
<li><a class="list-link">Blog</a></li>
<li><a class="list-link">Contact</a></li>
</ul>
转到domain.com/contact/email时,我想保留列表项以保持“活动”类。
答案 0 :(得分:1)
如果我正确理解了您的问题,那么如果当前浏览器路径中存在锚文本,那么您想添加x=[[['key1','value1'], ['key2','value2'], ['key3','value3'], ['key2','value20']],
[['key1','value4'], ['key3','value6'], ['key4','value2']]]
def make_df(sublist):
keys=[i[0] for i in sublist]
keys=[str(keys[i])+'_'+str(keys[i:].count(keys[i])-1) for i in range(len(keys))]
vals=[[i[1]] for i in sublist]
df=pd.DataFrame({k:v for k,v in zip(keys, vals)})
return df
df_list=[make_df(i) for i in x]
for i in df_list:
print(i)
key1_0 key2_1 key3_0 key2_0
0 value1 value2 value3 value20
key1_0 key3_0 key4_0
0 value4 value6 value2
def combine_dfs(df_lst):
df=df_lst[0]
for i in df_lst[1:]:
df=pd.concat([df,df_list[1]], axis=0, sort=True)
return df
new_df=combine_dfs(df_list)
new_df
key1_0 key2_0 key2_1 key3_0 key4_0
0 value1 value20 value2 value3 NaN
0 value4 NaN NaN value6 value2
类以锚定active
列表中的元素。
可以通过以下方式实现(有关详细信息,请参见摘录中的文档):
ul
jQuery(function($) {
//var path = window.location.pathname;
var path = 'domain.com/contact/email';
// Convert path to lower case for easier matching
path = path.toLowerCase();
$('ul a').each(function() {
// Extract link label via text() method. Convert
// label to lower case for easier matching
var label = $(this).text().toLowerCase();
// Use indexOf() to check for label existing in
// path
if( path.indexOf(label) !== -1 ) {
// Add active class if match found
$(this).addClass('active');
}
});
});
.active {
background:red;
}
更新
我找到了使用此jQuery代码所需的内容:
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<ul>
<li><a class="list-link">Posts</a></li>
<li><a class="list-link">Blog</a></li>
<li><a class="list-link">Contact</a></li>
</ul>
答案 1 :(得分:0)
锚点没有pathname
属性。更改该行以检查锚点中包含的文本:
if (this.textContent.indexOf( path )) {