How to add CSS if element has more than one child?

前端 未结 6 1813
不思量自难忘°
不思量自难忘° 2020-12-28 14:40

I have td tags and a several div inside td:


   
6条回答
  •  南笙
    南笙 (楼主)
    2020-12-28 15:25

    i think there is no way to add the 10px margin to each div inside a td without the use of css3.

    so a solution would be to use javascript and check if there are more than 1 div's inside the td and then if yes add a special class.

    css

    .myMarginClass div{
     margin-bottom:10px;
    }
    

    js

    var td=document.getElementsByTagName('td'),
    l=td.length;
    while(l--){
     if(td[l].getElementsByTagName('div').length>1){
      td[l].className='myMarginClass';
     }
    }
    

    else for modern browsers the proper solution is the :only-child proposed by @mikel

提交回复
热议问题