2012-03-06 11 views
1

私はディレクトリにファイルを一覧表示するワードプレスサイトを持っています。コードを再書き込みキャメルケースへのリンク表示テキスト、拡張子なし、スペースにはjQueryを使用してのまわりで私の頭をラップしようとしているjQueryアンカータグのテキスト書き直しプロジェクト

<a href="http://helpdesk-3/acme/wp-content/uploads/important_documents/compliance_documents/sample_document.pdf">sample_document.pdf</a> 

としてイムを吐き出すます。だからそれを実行して、それを書き直すだろう

<a href="http://helpdesk-3/acme/wp-content/uploads/important_documents/compliance_documents/sample_document.pdf">Sample Document</a> 

誰か提案がありますか?

+0

両方の例であなたのHTMLは同じに見えますか? –

+0

ページ内のすべてのリンクを検索...または特定のコンテナ内のこれらのリンクですか? – charlietfl

+0

@DavidThomas「a」内のテキストが異なります。 –

答えて

3
$('a').each(function() { // Loop over all links 
    var txt = $(this).text().split('.'); // Link contents, split on '.' 
    txt.pop(); // remove last (the extension) 
    txt = txt.join('.').replace(/_/g, ' '); // replace '_' with spaces 
    txt = $.map(txt.split(' '), function(v) { // split on spaces 
     // and uppercase the 1st letter 
     return v.substring(0, 1).toUpperCase() + v.substring(1, v.length); 
    }).join(' '); 
    $(this).text(txt); // set the new text 
});​ 

DEMO:http://jsfiddle.net/f6x9P/1/

+0

これを代わりに、私の答えを削除しました。 –

+0

完璧に作業しました。コード内のコメントをありがとう、私はそれを学ぶのに役立ちます。 –

+0

@EricHastings:あなたが大歓迎です:-) –

0
//setup function to capitalize each word in a string, breaking the string at the underscore characters 
function capitalize (str) { 

     //get each word 
     var split = str.split('_'); 

     //loop through the words 
     for (var i = 0, len = split.length; i < len; i++) { 

      //set the first letter to uppercase, and the rest to lowercase 
      split[i] = split[i].charAt(0).toUpperCase() + split[i].slice(1).toLowerCase(); 
     } 

     //now return the new string, words separated by spaces 
     return split.join(' '); 
} 

//select the elements you want to update, then update their text 
$('a').text(function (index, oldText) { 
    return capitalize(oldText); 
}); 

これは、スペースの世話をします。

.text()に関数を渡すと、選択した要素をループしてテキストを更新するよう求められます。あなたが何であれreturnが新しいテキストになり、無名関数に選択項目の現在の要素のインデックスと現在の要素の値が渡されます:http://api.jquery.com/text

1

Wordpressを使用しているので、 PHP。

<?php 
    $doc = 'sample_document.pdf'; 
    $array = explode('.', $doc); 
    $string = ucwords(str_replace('_', ' ', $array[0])); 
    echo $string; 
+0

'$ array [0]'は、ファイルに '.'sが複数ある場合は動作しません。 'sample_document_2.0.pdf'に似ています。 –

+1

次に、拡張子を削除して配列を改変して、スペースを置き換えて編集します。 '<?php \t $ doc = sample_document.pdf; \t $ array = explode( '。'、$ str); \t $ noExt = array_pop($ array); \t $ string = ucwords(str_replace( '_'、 ''、implode( '。'、$ noExt))); \t echo $ string; ?> ' –

0

可能性が非常に高い必要が

$('a').each(function() { 
    var $this = $(this); 
    var txtArr = $this.text().split('.')[0].split('_'); 
    for (i = 0; i < txtArr.length; i++) { 
     txtArr[i] = txtArr[i].charAt(0).toUpperCase() + txtArr[i].slice(1); 
    } 
    $this.text(txtArr.join(' ')); 
}); 
0

確かにページ内のすべてのタグをループするよりもはるかに優れたセレクタを作成します。しかし、strainght javascriptは文字列の操作に使用されます。そして、どうやらあなたはPDFファイルの拡張子を取り除くしたい

atext = $('a').text();

:あなたはアンカーのテキスト値を取得したいです。私は、ファイル名に一つだけの期間があると仮定します、と拡張子は何もすることができます:[0]今期間の左にあるすべてのものが含まれていatext

atext = atext.split('.');

atext = atext[0].split('_'); camel = ''

for word in atext { 
    w = word.substring(0,1).toUppercase(); 
    w = w + word.substring(1); 
    camel = camel + w + ' '; 
} 

最後に、タグ内のテキスト変更:あなたがここに改善する必要があります事があります

$('a').text(camel);

をそのアンダースコアが区切り文字であると仮定します。 jQueryを使用して 'a'を取得すると、すべてのアンカーが返されます。より具体的に一致文字列を変更してください。または、すべてのアンカータグを変更する場合は、すべての文字列操作コードを関数に入れ、一致するすべてのアンカータグに適用する必要があります。

関連する問題