I have a subnav which I am using the text in the subnav to reference a class of the DIV element.
some of the subnav text has spaces so for the ones that have spaces I just want to use the first word only to reference the class of the DIV element.
SCRIPT
$('li.' + $(this).text( function () {
if space
take first letter only.toggle(true);
if no space
do nothing.toggle(true);
});
HTML
<ul>
<li>all</li>
<li>men</li>
<li>women</li>
<li>sun protective clothing<开发者_如何转开发/li>
</ul>
<div class="men"></div>
<div class="women"></div>
<div class="sun"></div>
Thanks in advance!
Use the javascript string split method : http://www.w3schools.com/jsref/jsref_split.asp
var className = $(this).text().split(' ')[0];
精彩评论