猿问

在切换时更改 url

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');

        }

    });

});


慕妹3242003
浏览 153回答 2
2回答

明月笑刀无情

假设 'custom-switch' 类针对你的三个按钮,你可以试试这个:jQuery(document).ready(function () {&nbsp; &nbsp; jQuery('.custom-switch').click(function(){&nbsp; &nbsp; &nbsp; &nbsp; var target = jQuery('.anchr'); // this is less wasteful&nbsp; &nbsp; &nbsp; &nbsp; var url = target[0].attr('href'); // notice the indices&nbsp; &nbsp; &nbsp; &nbsp; var src = target[0].attr('data-source');&nbsp; &nbsp; &nbsp; &nbsp; var type = target[0].attr('data-target');&nbsp; &nbsp; &nbsp; &nbsp; if(type == 'monthly'){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; target[1].attr('href', src);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; target[1].attr('data-source', url);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; target[1].attr('data-target', 'yearly');&nbsp; &nbsp; &nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; target[2].attr('href', src);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; target[2].attr('data-source', url);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; target[2].attr('data-target', 'monthly');&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });});
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答