Replace a certain character in Javascript
Am currently running this script (the original author is no longer supporting it):
http://askthecssguy.com/articles/checkbox-filters-with-jquery/
This works fine apart from if you have a tag (these in tern make each of the check boxes) which has spaces in it (if you have spaces it creates multiple tags). The only way to get around this is by using underscores which is fine however looks rubbish on the ren开发者_Python百科dered page. I was wondering is there a way after this script is run to remove all _
from certain <li>
classes and replace them with a space " ".
jQuery(".filters li label").each(
function(){
var elem = jQuery(this);
var txt = elem.html();
if(txt.indexOf("_")>-1){
elem.html( txt.replace(/_/g," ") );
}
}
);
Let's say your list item elements have a class "tagFilter" applied. Then you can do the following to remove underscores from the text of the list item:
$('.tagFilter').each(function(i,v) {
this.innerText = this.innerText.replace(/_/g, ' ');
});
Note, however, this assumes that your list item doesn't contain any other child nodes except a text node. That is:
<li class="tagFilter">my_tag_with_spaces</li>
If you have a specific child element of the list item that contains the text, say a label element, then you can do:
$('.tagFilter label').each(function(i,v) {
this.innerText = this.innerText.replace(/_/g, ' ');
});
精彩评论