admin管理员组

文章数量:1404599

I would like to hide the home menu item from my navigation menu and only display it when the mobile navigation menu is toggled. Is there a way that I can select the li item by anchor title (home) and add the active class to it when toggled, like I have done to the other elements? or could I do this with css somehow? I'm using a wordpress nav menu so I can't add a specific class to it. Many thanks.

    $(document).ready(function() {
    $('body').addClass('js');
    var $menu = $('#menu'),
    $logo = $('.logo'),
    $menulink = $('.menu-link');

    $menulink.click(function() {
    $menulink.toggleClass('active');
    $menu.toggleClass('active');
    $logo.toggleClass('active');
    return false;
    });
});

I would like to hide the home menu item from my navigation menu and only display it when the mobile navigation menu is toggled. Is there a way that I can select the li item by anchor title (home) and add the active class to it when toggled, like I have done to the other elements? or could I do this with css somehow? I'm using a wordpress nav menu so I can't add a specific class to it. Many thanks.

    $(document).ready(function() {
    $('body').addClass('js');
    var $menu = $('#menu'),
    $logo = $('.logo'),
    $menulink = $('.menu-link');

    $menulink.click(function() {
    $menulink.toggleClass('active');
    $menu.toggleClass('active');
    $logo.toggleClass('active');
    return false;
    });
});
Share Improve this question edited Jun 4, 2013 at 21:50 Joey 1,67916 silver badges30 bronze badges asked Jun 4, 2013 at 21:39 ladygeekgeekladygeekgeek 631 silver badge6 bronze badges 2
  • "Anchor title"? What does your HTML look like? – Niet the Dark Absol Commented Jun 4, 2013 at 21:41
  • possible duplicate of Find all elements with a certain attribute value in jquery – Felix Kling Commented Jun 4, 2013 at 21:55
Add a ment  | 

3 Answers 3

Reset to default 6

This will get the anchor with the title of "home"

$('a[title="home"]')

So you would use

$('a[title="home"]').toggleClass('active');

See the W3C selectors reference for more on this syntax

You know you can target an anchor attributed of a title with only css ?

a[title^="Some title text"] { color: red; }

For targeting with javascript --> related

var links = top.document.getElementsByTagName('a'); var result = []; var linkcount = links.length; for ( var i = 0; i < linkcount; i++) { if (links[i].getAttribute('title') === 'some title text here') { result.push(links[i]); } }

For targeting with jQuery --> user John Conde answered before or Get element by title jQuery

$('a[title="Some title text"]')

Also, try a search on the net with your question --> google for an example

$(document).ready(function() {
    $('body').addClass('js');
    var $menu = $('#menu'),
    $logo = $('.logo'),
    $menulink = $('.menu-link');
    $homelink = $('li[title*="home"]'); // remove the * if u have more with "*home*"

    $menulink.click(function() {
        $menulink.toggleClass('active');
        $menu.toggleClass('active');
        $logo.toggleClass('active');
        $homelink.toggleClass('active');
        return false;
    });
});

本文标签: jqueryTarget an element by its anchor title in javascriptStack Overflow