jQuery anchor 标记文本重写项目

标签 jquery regex wordpress

我有一个 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
});​

演示:http://jsfiddle.net/f6x9P/1/

关于jQuery anchor 标记文本重写项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9591186/

相关文章:

javascript - 向 Javascript/Jquery 添加多个属性

javascript - 如何在不刷新的情况下编辑和删除MySQL数据库的记录?

javascript - 如何将标签包裹在字符串中的标签周围 - Javascript

regex - 如果该行以模式开头,则替换第 n 列中的数据的命令

regex - Emacs Lisp 相当于 JavaScript 的 RegExp#exec

javascript - Youtube API/JS 选择要播放的视频时出现问题

php - 在 WooCommerce 3.3 的管理员订单列表中显示退回订单备注

javascript - 从选择中选择一个选项,然后使用 Codeigniter 在另一个选择框中显示数据

javascript - 如何将 iFrame 的部分 URL 更改为随机字符串?

javascript - 在后端复制 Wordpress 标题