检查href是否具有课程,如果有,请获取ID [英] Check to see if href has class, if so, grab the id

查看:38
本文介绍了检查href是否具有课程,如果有,请获取ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一些jQuery代码的帮助.

Im looking for a bit of help with some jQuery code.

基本上我有这样的HTML结构.

Basically i have a HTML structure like so.

<ul class="menu">
    <li><a href="#" id="parent-one">One</a></li>
    <li><a href="#" id="parent-two">Two</a></li>
    <li><a href="#" id="parent-three">Three</a></li>
    <li><a href="#" id="parent-four">Four</a></li>
</ul>

如果用户在其上,我的PHP代码会添加一个class ="active",因此,取决于单击的是哪个链接,它看起来像这样:

My PHP code adds a class="active" if the user is 'on' it, so depending on which of the links is clicked it will look like this:

<li><a href="#" class="active" id="parent-four">Four</a></li>

因此,一切都很简单

我想在页面加载时执行的操作是搜索每个菜单项,直到找到具有class ="active"的菜单项.然后获取该链接的ID,将其从parent-xxx更改为child -xxx,然后显示"该子ID.

What i want to be able to do on page load, is search through each menu item until it finds the one with class="active" .. then take the ID of that link, change it from parent-xxx to child-xxx and then 'show' that child ID.

这可能吗?

推荐答案

$(function() {
    // Within .menu, find .active, grab its id, and replace parent with child
    var new_id = $('.menu').find('.active').attr('id').replace('parent', 'child');

    // Select element with this new id, and show it.
    $('#' + new_id).show();
});​

提琴: http://jsfiddle.net/rwgJc/1/

这篇关于检查href是否具有课程,如果有,请获取ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆