2012-03-06 73 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'将返回所有的锚;所以请将匹配字符串更改为更具体。或者,如果您确实需要更改所有定位标记,则需要将所有字符串操作代码放入函数中,并将其应用于所有匹配的定位标记。