开发者

Remove whitespace between strings for all browsers?

Anyone know how to remove white space between strings for all browsers? This works in everything but IE7. Basically all the li's text is used to reference div's class to show them. Some li's text has spaces some do not.

SCRIPT

$('li.' + $(this).text().replace(/ /g, "-")).toggle(true);

HTML

<ul>
<li class="button" id="sun">sun&nbsp;protective&nbsp;clothing</li>
<li class="button" id="lawn">lawn&nbsp;games</li>
<li class="button" id="stuff">stuff</li>
</ul>

<div class="sun-protective-clothing"></div>
<div class="lawn-games"&g开发者_Python百科t;</div>
<div class="stuff"></div>

Thanks in advance!


You can use the unicode equivalent of &nbsp = u00a0

Now works great in IE7 too! :)

example DEMO

$('li').text(function (i, v) {
    return v.replace(/\u00a0/g,'-');
});


Try using /\s*/g as your pattern instead of an actual space. (The \s is used for space character(s) in regex). i.e.

$('li.' + $(this).text().replace(/\s*/g, "-")).toggle(true);

I assume you're referencing the text of the <LI> to refer to the class of the <DIV> element, and want the hyphens to be inserted.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜