我的HTML

<span id="search">filter for customers</span>


SCRIPT

$('#search').css('text-transform','capitalize');


这给我的结果是

<span id="search">Filter For Customers</span>


我的查询是如何排除大写的“ for”一词。这样的结果必须是

<span id="search">Filter for Customers</span>


请注意,这仅是示例,我列出了不能大写的单词列表。我正在寻找一些动态解决方案,只要找到这些单词,就不应将其用text-transform大写。
谢谢。

最佳答案

一种方法是循环播放所有文本,并检查其默认设置是否较低。大文本时此方法很慢



String.prototype.ucFirst = function()
{
    return this.charAt(0).toUpperCase() + this.substr(1);
};

$(document).ready(function () {
  var lowerWords = [
      'for', 'to'
  ];

  $('.uc-first').each(function () {
    var words = $(this).text().split(' ');
      pattern = /([^a-z0-9]*)(\w+)([^\w]*)/i

    $.each(words, function (i, value) {
      mathches = pattern.exec(value);
      if (lowerWords.indexOf(mathches[2]) == -1) {
        words[i] = mathches[1]+mathches[2].ucFirst()+mathches[3];
      }
    });

    $(this).text(words.join(' '));
  })
})

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span class="uc-first">some text for SO Community to investigate</span><br/>
<span class="uc-first">test ;for. additional symbols;</span><br/>
<span class="uc-first">filter for customers</span>

10-07 12:41
查看更多