Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jQuery锚定标记文本重写项目_Jquery_Regex_Wordpress - Fatal编程技术网

jQuery锚定标记文本重写项目

jQuery锚定标记文本重写项目,jquery,regex,wordpress,Jquery,Regex,Wordpress,我有一个wordpress站点,im在一个目录中列出文件。代码会被当作 <a href="http://helpdesk-3/acme/wp-content/uploads/important_documents/compliance_documents/sample_document.pdf">sample_document.pdf</a> 我正试图用jQuery将链接显示文本重新写入camel大小写、无扩展名和空格。所以它会运行并重新编写为 <a href

我有一个wordpress站点,im在一个目录中列出文件。代码会被当作

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

我正试图用jQuery将链接显示文本重新写入camel大小写、无扩展名和空格。所以它会运行并重新编写为

<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
});​
演示:

演示:

//设置函数,将字符串中的每个单词大写,并在下划线处断开字符串
函数大写(str){
//听懂每一个字
var split=str.split(“”“);
//循环读单词
对于(变量i=0,len=split.length;i
这将照顾空间

当您传递
.text()
函数时,您要求循环选择的元素,并更新它们的文本。无论您返回什么,都将是新文本,并且匿名函数将传递选择中当前元素的索引和当前元素的值:

//设置函数以大写字符串中的每个单词,在下划线处断开字符串
函数大写(str){
//听懂每一个字
var split=str.split(“”“);
//循环读单词
对于(变量i=0,len=split.length;i
这将照顾空间


当您传递
.text()
函数时,您要求循环选择的元素,并更新它们的文本。无论您返回什么,都将是新文本,并且匿名函数将传递当前元素在选择中的索引和当前元素的值:

因为您使用的是Wordpress,所以只使用PHP可能会更容易

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

既然您使用的是Wordpress,那么只使用PHP可能会更容易

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

很可能需要创建一个比在页面中循环每个标签要好得多的选择器

$('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(' '));
});
$('a')。每个(函数(){
var$this=$(this);
var txtArr=$this.text().split('.')[0].split('.');
对于(i=0;i
很可能需要创建一个比在页面中循环每个标签要好得多的选择器

$('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(' '));
});
$('a')。每个(函数(){
var$this=$(this);
var txtArr=$this.text().split('.')[0].split('.');
对于(i=0;i
当然可以。但strainght javascript用于字符串操作。要获取定位点的文本值,请执行以下操作:

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

显然你想去掉pdf的扩展名。我假设文件名中只有一个句点,扩展名可以是任何内容:

atext=atext.split('.')

atext[0]现在包含句点左侧的所有内容。假设下划线是分隔符:

atext=atext[0]。拆分(“”)
camel=''

最后更改标记中的文本:

$('a')。文本(驼峰)


在这里你需要改进一些事情。使用jQuery获取“a”将返回所有锚;因此,将匹配字符串更改为更具体。或者,如果确实要更改所有锚定标记,则需要将所有字符串操作代码放入函数中,并将其应用于所有匹配的锚定标记。

当然。但strainght javascript用于字符串操作。要获取定位点的文本值,请执行以下操作:

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

显然你想去掉pdf的扩展名。我假设文件名中只有一个句点,扩展名可以是任何内容:

atext=atext.split('.')

atext[0]现在包含句点左侧的所有内容。假设下划线是分隔符:

atext=atext[0]。拆分(“”)
camel=''

最后更改标记中的文本:

$('a')。文本(驼峰)


在这里你需要改进一些事情。使用jQuery获取“a”将返回所有锚;因此,将匹配字符串更改为更具体。或者,如果要更改所有定位标记,然后,您需要将所有字符串操作代码放入一个函数中,并将其应用于所有匹配的锚定标记。

两个示例中的html看起来是相同的?搜索页面中的每个链接……还是这些链接都在某个容器中?@DavidThomas
a
中的文本不同。@DavidThomas我认为OP指的是锚定而不是href,因为这是两个示例之间唯一不同的地方——尽管标题的表述不同。@JamesMontagne:啊,我被问题标题的
href
部分误导了。愚蠢的我…=/另外:这不是camelCase…两个示例中的html看起来是一样的?搜索页面中的每个链接…还是这些链接都在某个容器中?@DavidThomas
a
中的文本不同。@DavidThomas我想OP指的是锚的内容,而不是