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
3 Answers
Reset to default 6This 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
版权声明:本文标题:jquery - Target an element by its anchor title in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744829630a2627272.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论