0

これはすべて大文字の私のメニューです

<ul class="tabgic">
   <li rel="item_227" class="">
    <div>
      <div> <a class="menu_link_2" href="#">ACCIDENTAL DAMAGE AND PROPERTY</a> </div>
    </div>
   </li> 
</ul>

jQueryを使用して、単語以外のすべてを大文字にしてand、出力が次のようになるようにします。

Accidental Damage and Property

これどうやってするの?

私はこれを見ていましが、これを簡単に変更できるかどうかわかりませんか?

4

4 に答える 4

4

正規表現を使用できます。

str = str.toLowerCase().replace(/\b[a-z]/g, function(match) {
    return match.toUpperCase();
}).replace(/\bAnd\b/g, "and");

jsFiddle

CSSだけでそれを行うことはできません(少なくとも現在持っているマークアップではできません)。

jsFiddle

于 2012-10-11T03:19:46.907 に答える
0
var str = 'ACCIDENTAL DAMAGE AND PROPERTY';
var result = str.split(' ').map(function(v){
  v = v.toLowerCase();
  return v.replace(/^[a-z]/, function(a){
    return v === 'and' ? a : a.toUpperCase();
  });
}).join(' ');

console.log(result); //=> Accidental Damage and Property
于 2012-10-11T03:23:54.190 に答える
0

提案された出力を考えると、実際にはタイトルケース(適切なケースとも呼ばれます)が必要だと思います。リンクされたプラグイン(タイトルケースを処理します)を使用することも、正規表現を使用して独自のプラグインをロールすることもできます。

// the RegExp  \w{4,}  will capture any word composed of 4 or more characters
// where each character can match A-Z, a-z, 0-9, and _
myString = myString.toLowerCase().replace(/\w{4,}/g, function (match) {
    return match.substring(0, 1).toUpperCase() + match.substring(1);
});
于 2012-10-11T03:25:42.190 に答える
0

作業デモ http://jsfiddle.net/pXe44/

それが原因に合うことを願っています:)

コード

$(document).ready(function() {
    var foo = $('.menu_link_2').text().split(' ');
    var html = '';
    $.each(foo, function() {
        if (this.toLowerCase() != "and") html += this.substring(0, 1).toUpperCase() + this.substring(1).toLowerCase() + ' ';
        else html += this.toLowerCase() + ' ';

    });

    alert(" ===> " + html);

    $('.menu_link_2').html(html);

});​
于 2012-10-11T03:30:02.887 に答える