开发者

Only use first word using .text()

开发者 https://www.devze.com 2023-03-18 05:56 出处:网络
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

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

精彩评论

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