开发者

jQuery replace space with _ in options class

开发者 https://www.devze.com 2023-03-22 20:52 出处:网络
I have several options classes that have spaces <option value=\"Divers RF\" class=\"Divers RF\">Divers RF</option>

I have several options classes that have spaces

<option value="Divers RF" class="Divers RF">Divers RF</option>
<option value="France Bleu" class="France Bleu">France Bleu</option>

How can i replace the space with a _ 开发者_JAVA百科in the class?

so the result would be:

<option value="Divers RF" class="Divers_RF">Divers RF</option>
<option value="France Bleu" class="France_Bleu">France Bleu</option>

and not only for theses 2 classes but for all classes that have spaces in it.


$("option").each(function() {
  $(this).attr("class", $(this).attr("class").replace(/ /g, '_'));
});


$('option').each(function() {
     if($(this).attr('class') != "")
     {
          var className = $(this).attr('class');
          $(this).attr("class","");
          className = className.replace(/ /g,"_");
          $(this).addClass(className);
     }
});
0

精彩评论

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