html JavaScript question. Making simple sentences simple

Asked 6 days ago, Updated 6 days ago, 1 views

I want to change this code to a simple for statement, but I want to know how to change it. I want to know how to reduce it in other waysYo

  $(function(){
        $('#main_a1').click(function(){
            $('#main_content1').show();
        });

        $('#main_a2').click(function(){
            $('.main_content_wrap').hide();
            $('#main_content2').show();
        });

        $('#main_a3').click(function(){
            $('.main_content_wrap').hide();
            $('#main_content3').show();
        });
       } to 81 });

javascript for

2022-09-20 15:18

1 Answers

It's easy to fix a given script as a circuit.

for (i = 1; i < 82; i++) {
    let eventTargetID = '#main_a' + i;
    let showTargetID = '#main_content' + i;
    $(eventTargetID).click(function(){
        $(showTargetID).show();
    });
}

But it needs to be 82 later onOr, it should be about 60,000 or none, so it should be removed and made new, or from the 13th, it should be in the form of #main_vip13... If we do that, it will be difficult to solve the problem with just for, right?

I think you're implementing tab navigation and I'd give each element a specific inevitable association and then refer to it in the script as 'generally'

<!-- No id is required in this example. See the script below -->
<a class="main_clicker" data-what-to-show="#main_content3">Tap3</a>
// Just give the main_clicker class and data-what-to-show properties to any element and the code below applies.
// That element doesn't have to be a, it doesn't have to be an element ID.
// Whether you have 82 or 820,000 of those elements, they'll all work the same way.
// It's common in this sense.
$('.main_clicker').click(function (e) {
    let id = $(e.target).data('whatToShow');
    $(id).show();
});


2022-09-20 15:18

If you have any answers or tips


© 2022 pinfo. All rights reserved.