Jquery在飞行链接上更改扩展名

时间:2016-06-09 06:34:56

标签: jquery

我在很多页面中都设置了链接。 例如:

<ul>
 <li> <a href="someurl/somefile.html"> Some file </a> </li>
 <li> <a href="someurl/somefile1.html"> Some file1 </a></li>
 <li> <a href="someurl/somefile2.html"> Some file2 </a></li>
</ul>

<a href="someurl/someotherfile.html"> Some Other file </a>
<a href="someurl/someotherfile1.html"> Some Other file1 </a>

现在,我想将html更改为php扩展名。无需修改php代码。

我尝试过以下代码但不起作用。

$('a').each(function() {
        $(this).html( $(this).html().replace( ".php", '.html' ) );
 });

任何帮助将不胜感激。

4 个答案:

答案 0 :(得分:4)

您的代码中有两个问题:

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

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

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

工作代码段

&#13;
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul> 
 <li> <a href="someurl/somefile.html" target="_blank"> Some file </a> </li>
 <li> <a href="someurl/somefile1.html" target="_blank"> Some file1 </a></li>
 <li> <a href="someurl/somefile2.html" target="_blank"> Some file2 </a></li>
</ul>

<a href="someurl/someotherfile.html" target="_blank"> Some Other file </a>
<a href="someurl/someotherfile1.html" target="_blank"> Some Other file1 </a>
&#13;
 in_pycharm = 'original_argv' in dir(sys) and 'pydevd' in sys.original_argv[0]
&#13;
&#13;
&#13;

答案 1 :(得分:1)

只需使用Slider,如下所示:

wrapper.setOnKeyPressed(ke -> {            
    int angle = 0;
    Rotate orangeCircleRotation = new Rotate(0, CIRCLES_CENTER_X, CIRCLES_CENTER_Y);
    orangeCircle.getTransforms().add(orangeCircleRotation);
    Rotate yellowCircleRotation = new Rotate(0, CIRCLES_CENTER_X, CIRCLES_CENTER_Y);
    yellowCircle.getTransforms().add(yellowCircleRotation);
    Timeline rotationAnimation = new Timeline();
    if (ke.getCode().toString().equals("RIGHT")) {
        angle = -36;
    }
    if (ke.getCode().toString().equals("Left")) {
        angle = 36;
    }
    rotationAnimation.getKeyFrames().add(
                    new KeyFrame(Duration.millis(100),
                            new KeyValue(orangeCircleRotation.angleProperty(), angle)));
    rotationAnimation.getKeyFrames().add(
                    new KeyFrame(Duration.millis(100),
                            new KeyValue(yellowCircleRotation.angleProperty(), angle)));
    rotationAnimation.play();
}
replace

答案 2 :(得分:0)

你应该使用,

$('a').each(function() {
    var href = $(this).attr('href');
    href = href.replace(".php", '.html') ;
    $(this).attr("href", href);
});

希望这个答案可以帮到你。

答案 3 :(得分:0)

  

尝试使用此代码。

var href = $(this).attr('href');
href = href.replace( ".php", '.html' ) ;
$(this).attr("href", href);