开发者

Only use first word using .text()

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];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜