gpt4 book ai didi

jquery - 如何使用jquery查找和替换某些内容?

转载 作者:行者123 更新时间:2023-12-01 03:09:02 25 4
gpt4 key购买 nike

我在很多页面中设置了以下链接。

以下是包含代码的 php 文件,即:

输入:

  <a href="someurl/someotherfile.html"> Some Other file </a>
<a href="someurl/someotherfile1.html"> Some Other file1 </a>
<p><a href="someurl/someotherfile2.html"> Some Other file2 </a></p>
<div><a href="someurl/someotherfile2.html"> Some Other file2 </a></div>
<span><a href="someurl/someotherfile2.html"> Some Other file2 </a></span>

输出:

  <a href="someurl/someotherfile.php"> Some Other file </a>
<a href="someurl/someotherfile1.php"> Some Other file1 </a>
<p><a href="someurl/someotherfile2.php"> Some Other file2 </a></p>
<div><a href="someurl/someotherfile2.php"> Some Other file2 </a></div>
<span><a href="someurl/someotherfile2.php"> Some Other file2 </a></span>

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

我尝试了以下代码,但它不起作用。注意此代码中发生的情况。

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

最佳答案

尝试:

使用attr('href',value)设置新的href并使用.replace将html替换为php

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

关于jquery - 如何使用jquery查找和替换某些内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37789342/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com