替换 href 属性中文本的值

Replace value from text in href attribute

本文关键字:文本 中文 href 属性 替换      更新时间:2023-09-26

当 DOM 准备就绪时,我正在尝试将 expand 的值更改为 0。

我尝试过不同的东西

$('.link-comment').attr('href').find("expand=1").replaceWith('expand=0');

但它不起作用。所以我可以将扩展的值更改为 0

<a class="link-comment" href="/eventcomments/create-like/227?expand=1">

试试这个方式:

$(function(){
    $('.link-comment').attr('href', function (_, cur) {
        return cur.replace(/expand=1/, "expand=0");
    });
});

小提琴

尝试

$('.link-comment').filter('[href*="expand=1]"').attr('href', function(idx, href){
    return href.replace('expand=1', 'expand=0')
})

试试这个:

$('.link-comment').attr('href',$('.link-comment').attr('href').replace("expand=1", "expand=0"));