我有一个 WordPress 站点,它列出了目录中的文件。代码被吐出为
<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>
有人有建议吗?
最佳答案
$('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
});
关于jQuery anchor 标记文本重写项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9591186/