用于访问嵌套div的CSS选择器组合

时间:2017-07-11 10:14:29

标签: html css css-selectors

我有以下HTML代码

<div id="left">
  <div id="coverage">
    <h3>coverage</h3>
    <!-- first div -->
    <div>    
      <div>
        <span>left->coverage->div 1->inner div 1->span text</span>
      </div>

      <div>
        <span>left->coverage->div 1->inner div 2->span text</span>
      </div>      
    </div>

    <!-- second div -->
    <div>

      <!-- div to be accessed -->
      <div>
          <span>left->coverage->div 2->inner div 1->span text</span>
      </div>    

    </div>
  </div>
</div>

我需要访问嵌套在第二个div中的第一个div。我试过这个,但它没有工作

#left #coverage  div:first-child
{
  color: red;
}

2 个答案:

答案 0 :(得分:2)

您可以使用div获得第二个:first-child#left #coverage > div:nth-of-type(2) div:first-child { color: red; }以获得第一个div。

&#13;
&#13;
<div id="left">
  <div id="coverage">
    <h3>coverage</h3>
    <!-- first div -->
    <div>
      <div>
        <span>left->coverage->div 1->inner div 1->span text</span>
      </div>

      <div>
        <span>left->coverage->div 1->inner div 2->span text</span>
      </div>
    </div>

    <!-- second div -->
    <div>

      <!-- div to be accessed -->
      <div>
        <span>left->coverage->div 2->inner div 1->span text</span>
      </div>

    </div>
  </div>
</div>
&#13;
public class ImportFragment extends Fragment {
ListView listView;
@Nullable
@Override
enter code here

 Button butt1=(Button)findViewById(R.id.btn1);

 butt1.setOnClickListener(new View.OnClickListener() {
 @Override
 public void onClick(View v) {
 Intent int1=new Intent(MainActivity.this,newactivity.class);
 startActivity(int1);
 finish();
 }
 });
 return rootView;
 }
}
&#13;
&#13;
&#13;

答案 1 :(得分:1)

foo = [ foorule("a"), foorule("b"), ] 应该适用于您的目的:

deadbeaf-0011-...
#left #coverage>div:nth-of-type(2)>div