Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/76.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 - Fatal编程技术网

动态链接上的Jquery更改扩展

动态链接上的Jquery更改扩展,jquery,Jquery,我有这么多的网页链接集。 例如: 任何帮助都将不胜感激。您的代码中有两个问题: 1) 您需要修改元素的属性href,而不是html 2) 您已经交换了用php替换html的参数。第一个参数应该用于匹配,第二个参数用于替换 $('a').attr('href',function(i,oldhref) { return oldhref.replace( ".html",'.php'); }); 工作片段: $(函数(){ $('a').attr('href',function(i,old

我有这么多的网页链接集。 例如:


任何帮助都将不胜感激。

您的代码中有两个问题:

1) 您需要修改元素的属性href,而不是html

2) 您已经交换了用php替换html的参数。第一个参数应该用于匹配,第二个参数用于替换

$('a').attr('href',function(i,oldhref) {
    return oldhref.replace( ".html",'.php');
});
工作片段:

$(函数(){
$('a').attr('href',function(i,oldhref){
返回oldhref.replace(“.html”,“.php”);
});
});


只需使用
替换
,如下所示:

$(文档).ready(函数(){
$('a')。每个(函数(){
$(this.attr('href',$(this.attr('href').replace('html','php'));
});
})

您应该使用

$('a').each(function() {
    var href = $(this).attr('href');
    href = href.replace(".php", '.html') ;
    $(this).attr("href", href);
});
希望这个答案对您有所帮助。

请尝试使用此代码

随时快乐编码…)我觉得你应该接受@Milindantwar的回答。。因为它简单而优雅。。
$('a').attr('href',function(i,oldhref) {
    return oldhref.replace( ".html",'.php');
});
$('a').each(function() {
    var href = $(this).attr('href');
    href = href.replace(".php", '.html') ;
    $(this).attr("href", href);
});
var href = $(this).attr('href');
href = href.replace( ".php", '.html' ) ;
$(this).attr("href", href);