简体   繁体   English

CSS仅通过同级选择器选择元素

[英]CSS only select an element by a sibling's selector

I have this html (snipped for brevity) 我有这个html(为简洁起见)

<label>
    <div class="label">Condition</div>
    <select>
        <option value="blah" selected="selected">Blah</option> 
        <option value="other...">other...</option>
    </select>
    <input type="text" name="condition" value="" style="display: none;">
</label>

I want to get the select element. 我想获取选择元素。 The only static/non-generic factor I can see is the input[name='condition'] but the generic sibling selector won't help be because the 'select' comes before the input. 我可以看到的唯一静态/非通用因素是input [name ='condition'],但是通用同级选择器将无济于事,因为'select'在输入之前。 So something like input[name='condition'] ~ select does not work. 因此,类似input[name='condition'] ~ select不起作用。

This element moves, sometimes is the first, sometimes the twentieth and can be any and all locations in between so nth isn't going to work. 此元素移动,有时是第一个,有时是第20个,并且可以在两者之间的任何位置,因此第n个将不起作用。

I know how to get this with Javascript/Jquery but that is not an option here. 我知道如何使用Javascript / Jquery来获取此信息,但这不是这里的选择。 I only have CSS selectors available. 我只有CSS选择器可用。 So the big question is, is there any way to get the 'select' out of this HTML using only CSS selectors? 因此,最大的问题是,是否有任何方法可以仅使用CSS选择器从HTML中获取“选择”?

Is there any way to get a select element that has an option element with a value='something'? 有什么办法可以得到一个选择元素,该选择元素的选项元素的值为value ='something'?

Try this css code: 试试下面的CSS代码:

div.label ~ select

It returns any selects that are preceded by a div with class label. 它返回所有带有类标签的div的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM