How to apply CSS style to all elements with same ID using one button?

后端 未结 6 529
小蘑菇
小蘑菇 2021-01-26 06:59

My idea was to create a button to hide/show all hidden text (Hide by highlighting the text in black, show by making the background color of the text transparent). But the codes,

6条回答
  •  再見小時候
    2021-01-26 07:30

    As others have said, do not use the same id more than once on a single page. Instead, use a class name or data attributes. And since you marked your question with jQuery:

    function change()
    {
        var test = document.getElementById("button1");
        if (test.value=="Hide Spoiler") 
        {
            test.value = "Open Spoiler"; 
            $('.className').css('background-color','transparent');
        }
        else 
        {
            test.value = "Hide Spoiler"; 
            $('.className').css('background-color','transparent');
            $('.className').css('color','black');
        }
    }
    

    If you're going to use jQuery, I'd also recommend using the button's click event like such:

    $('#button1').click(function() {
      ...
    });
    

提交回复
热议问题