jQuery match only part of id from div

前端 未结 6 1806
星月不相逢
星月不相逢 2021-02-01 12:51

I ran into an unusual situation yesterday night.

I need to match only part of id. Let me clear you all with an example

I have few divs like



        
6条回答
  •  囚心锁ツ
    2021-02-01 13:38

    You can use css selector

    try this code

    html

    css

    .red{
        display:block; 
        float:left; 
        width:50px; 
        height:20px; 
        background-color:#FF0000;}
    .blue{
        display:block; 
        float:left; 
        width:50px; 
        height:20px; 
        background-color:#0000FF;}
    

    jquery

    $('div[id^="sales"]').addClass('red');
    $('div[id^="user"]').addClass('blue');
    

    css selector ^= will select div with ID that start with the specified value and add the css style.

提交回复
热议问题