jquery help for div having same prefix id

前端 未结 3 1075
既然无缘
既然无缘 2021-01-14 05:08

I have 4 DIV\'s with different ID\'s (but have same prefix (testDiv[1|2|3|4]) ) and i want to set their visblity(visible or hidden) on some event. How can i use to set the

相关标签:
3条回答
  • 2021-01-14 05:55
    $('div[id^=testDiv]').attr('visibility', 'hidden');
    
    0 讨论(0)
  • 2021-01-14 05:56

    Having a common class is not the workaround. It is the right way to do it. The class establishes the relationship between these four elements so that you can manipulate them in a single go. The id is used to be able to identify them uniquely.

    0 讨论(0)
  • 2021-01-14 06:00

    You can use the starts with selector ^= like this:

    $('div[id^=testDiv]').css('visibility', 'visible'); 
    

    Description: Selects elements that have the specified attribute with a value beginning exactly with a given string.

    Note that if you use a class instead, you can target them easily but that depends on your requirements.

    0 讨论(0)
提交回复
热议问题