2012-03-06 76 views
1

我有一個WordPress列出了目錄中的文件的網站。該代碼被吐出的jQuery錨標記文本重寫項目

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

我試着環繞使用jQuery重新編寫鏈接顯示文本駱駝的情況下,沒有擴展名和空格我的頭。所以它會運行並重寫爲

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

有沒有人有建議?

+0

這兩個例子中的html看起來都一樣嗎? – 2012-03-06 20:19:26

+0

搜索頁面中的每一個鏈接...或者這些在某個容器中? – charlietfl 2012-03-06 20:20:34

+0

@DavidThomas「a」中的文字不同。 – 2012-03-06 20:21:31

回答

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

刪除我的答案,upvoting而不是。 – 2012-03-06 20:42:44

+0

完美工作。感謝代碼中的評論,幫助我學習它。 – 2012-03-06 20:44:21

+0

@EricHastings:不客氣,很高興我能幫忙:-) – 2012-03-06 20:46:24

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]'如果文件中有多個''''就不會工作。像'sample_document_2.0.pdf'。 – 2012-03-06 20:32:21

+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; ?> – 2012-03-06 20:39:15

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用於字符串操作。你想要得到的錨文本值:

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

那麼顯然你要剝去PDF格式擴展名。我會認爲只有一個在文件名期,延長可以是任何東西:

atext = atext.split('.');

atext [0]現在包含一切,留下一段。假設下劃線是分隔符:

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'將返回所有的錨;所以請將匹配字符串更改爲更具體。或者,如果您確實需要更改所有定位標記,則需要將所有字符串操作代碼放入函數中,並將其應用於所有匹配的定位標記。