且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

jQuery替换单词中的空格

更新时间:2023-02-23 10:28:11

简单的string.replace可以解决问题:

var str = "Test One";
str = str.replace(/ /g, '');

现在,关于您的问题,这里变得有些混乱.如果要替换 value属性,则:

Now with regards to your question, this is where it gets a little confusing. If you want to replace the value attribute, then:

$('option').val(function (i, value) {
    return value.replace(/ /g, '');
});

如果要在标签之间替换文本,则:

If you want to replace the text between the tags, then:

$('option').text(function (i, text) {
    return text.replace(/ /g, '');
});