为了账号安全,请及时绑定邮箱和手机立即绑定

在切换时更改 url

在切换时更改 url

慕妹3242003 2021-10-21 15:09:31
jQuery(document).ready(function () {    jQuery('.custom-switch').click(function(){        var url = jQuery('.anchr').attr('href');        var src = jQuery('.anchr').attr('data-source');        var type = jQuery('.anchr').attr('data-target');        if(type == 'monthly'){            jQuery('.anchr').attr('href', src);            jQuery('.anchr').attr('data-source', url);            jQuery('.anchr').attr('data-target', 'yearly');        }else{            jQuery('.anchr').attr('href', src);            jQuery('.anchr').attr('data-source', url);             jQuery('.anchr').attr('data-target', 'monthly');        }    });});<div class="switch"><span class="monthly">Billed Monthly</span> <div class="custom-switch" id="undefined"><input class="custom-switch" type="checkbox" id="custom-switch-0"><label for="custom-switch-0"></label></div> <span class="annually">Billed Annually (Save 10%) </span></div><ul><li class="plan__price"><a href="https://www.example.com/india/?amt=7499" data-source=https://www.example.com/india/?amt=6749" data-target="monthly" class="anchr btn btn-primary price-btn" target="_blank" rel="noopener"> Pay Now </a></li></ul>我使用了具有不同网址的两个立即付款按钮,切换后两个按钮都获得相同的网址。需要不同的网址,如果我再次为第二个按钮编写相同的代码,但效果不同,那么效果很好jQuery(document).ready(function () {    jQuery('.custom-switch').click(function(){        var url = jQuery('.anchr').attr('href');        var src = jQuery('.anchr').attr('data-source');        var type = jQuery('.anchr').attr('data-target');        if(type == 'monthly'){            jQuery('.anchr').attr('href', src);            jQuery('.anchr').attr('data-source', url);            jQuery('.anchr').attr('data-target', 'yearly');        }else{            jQuery('.anchr').attr('href', src);            jQuery('.anchr').attr('data-source', url);             jQuery('.anchr').attr('data-target', 'monthly');        }    });});
查看完整描述

2 回答

?
明月笑刀无情

TA贡献1828条经验 获得超4个赞

假设 'custom-switch' 类针对你的三个按钮,你可以试试这个:


jQuery(document).ready(function () {

    jQuery('.custom-switch').click(function(){

        var target = jQuery('.anchr'); // this is less wasteful

        var url = target[0].attr('href'); // notice the indices

        var src = target[0].attr('data-source');

        var type = target[0].attr('data-target');

        if(type == 'monthly'){

            target[1].attr('href', src);

            target[1].attr('data-source', url);

            target[1].attr('data-target', 'yearly');

        }else{

            target[2].attr('href', src);

            target[2].attr('data-source', url); 

            target[2].attr('data-target', 'monthly');

        }

    });

});


查看完整回答
反对 回复 2021-10-21
  • 2 回答
  • 0 关注
  • 152 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信