Jquery 将 url 作为变量打开并更改文本

Jquery open url as a variable and change text

本文关键字:文本 变量 url Jquery      更新时间:2023-09-26

>我有这个:

<fieldset id="social_associations" class="">
    <a href="/social/login/twitter">Associate your account to twitter</a>
    <br>
    <a href="/social/login/facebook">Associate your account to facebook</a>
    <br>
</fieldset>

我希望一个函数跟踪这些链接中的 URL,但这些 URL 是由脚本生成的,可能会根据用户的个人资料而更改(如果用户已经关联了一个帐户,那么将出现的 URL 将具有"断开连接"而不是"登录"(,因此该函数必须采用链接中显示的任何内容, 它不能硬编码。我不想在单击当前页面时离开它。当单击链接时,我想将文本从关联...以断开连接。一定很容易,但我不知道!

编辑:

我知道这个问题不清楚,所以我在这里改写了一下:JavaScript 单击更改 HRF 并在新窗口中打开

<script>
$(function(){
    $('#social_associations').each(function(){
        if(/this.href/.test(/login/i)){
            this.innerHTML = 'Login';
        }
        if(/this.href/.test(/disconnect/i)){
            this.innerHTML = 'Disconnect';
        }
    });
});
</script>
<fieldset id="social_associations" class="">
    <a href="/social/login/twitter">Associate your account to twitter</a>
    <br>
    <a href="/social/login/facebook">Associate your account to facebook</a>
    <br>
</fieldset>

我不确定这是否是你想要的,所以告诉我它是否不是:)

要更改链接文本,我会这样做。至于问题的其余部分,我不确定我是否理解。

$( '#social_associations' )
     .on( 'click', 'a[href*="/login/"]', function(){
             // not sure what 'follow the URLs within these links' means, but do it here
               //then change the link text
             $( this ).text( $( this ).text().replace( /Associate/i, "disconnect" ) );            
         })
     .on( 'click', 'a[href*="/disconnect/"]', function(){
               // not sure what 'follow the URLs within these links' means, but do it here
               //then change the link text
               $( this ).text( $( this ).text().replace( /disconnect/i, "Associate" ) );
         })