我想在 span
后面选择一个 input
标签:
HTML :
<input type="text"/>
<span>
...
</span>
CSS:
input [followed span] {
padding:5px; (set input padding, only when followed by span)
}
最佳答案
您可以使用 + 选择器来选择所有 <span>
紧跟在 <input>
之后的元素元素
input + span {
background-color: yellow;
}
<input type="text">
<span>test</span>
如果要选择<input>
放在 <span>
之前.这在 css 中是不可行的,因为,
Cascading Style Sheets : A cascade is like a waterfall, there’s no backward motion. So, naturally, there is no previous sibling selector in CSS.
阅读更多 https://www.wikitechy.com/technology/previous-sibling-css-selector/
你可以尝试的是 .prev()
jQuery 的..
$(document).ready(function(){
$('span').prev().css('background','yellow');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text">
<span>test</span>
有关 .prev()
的更多信息https://www.w3schools.com/jquery/traversing_prev.asp
关于jquery - 后跟跨度标签时如何选择输入标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48981222/