HTML
<div class="red">
<form>
<input class="s" placeholder="Search">
</form>
</div>
<div class="blue"> blue </div>
<div class="green"> green </div>
<div class="yellow"> yellow </div>
CSS
.red, .blue, .green, .yellow
{
padding: 10px;
border: 1px solid #f00;
}
.red{
background: red;
}
.blue{
background: blue;
}
.green{
background: green;
}
.yellow{
background: yellow;
}
.s:focus{
border: 1px solid black;
}
.s:focus + yellow{
display: none;
}
答案 0 :(得分:3)
用jquery做这件事很容易;
(function() {
$('.red input').on({
click: function() { // maybe you'd prefer on focus?
$('.yellow').fadeOut();
}
})
})();
答案 1 :(得分:1)
CSS中的+选择器仅适用于相邻元素。在这种情况下,您想要使用javascript。 JQuery让它变得特别简单:
$('.s').focus(function() {
$('.yellow').hide();
}). blur(function() {
$('.yellow').show();
});
这是修订后的JSFiddle:http://jsfiddle.net/w10cw8tc/
$(function(){
$('.s').focus(function() {
$('.yellow').hide();
}). blur(function() {
$('.yellow').show();
});
});
&#13;
.red, .blue, .green, .yellow {
padding: 10px;
border: 1px solid #f00;
}
.red{
background: red;
}
.blue{
background: blue;
}
.green{
background: green;
}
.yellow{
background: yellow;
}
.s:focus{
border: 1px solid black;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="red">
<form>
<input class="s" placeholder="Search" />
</form>
</div>
<div class="blue"> top </div>
<div class="green"> middle </div>
<div class="yellow"> bottom </div>
&#13;
答案 2 :(得分:1)
其他答案将有效。如果您愿意,这里有一个小提琴形式。
$(document).ready(function() {
$(".red input").focus(function() {
$(".yellow").hide();
});
});
答案 3 :(得分:1)
答案 4 :(得分:1)
试试这个
$(function(){
$(".s").on("focus",function()
{
$(".yellow").hide();
});
$(".s").on("blur",function()
{
$(".yellow").show();
});
});
&#13;
.red, .blue, .green, .yellow
{
padding: 10px;
border: 1px solid #f00;
}
.red{
background: red;
}
.blue{
background: blue;
}
.green{
background: green;
}
.yellow{
background: yellow;
}
.s:focus{
border: 1px solid black;
}
.s:focus + yellow{
display: none;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="red">
<form>
<input class="s" placeholder="Search">
</form>
</div>
<div class="blue"> top </div>
<div class="green"> middle </div>
<div class="yellow"> bottom </div>
&#13;
答案 5 :(得分:0)
使用jQuery:
$('#search').on('focus', function () {
$('#yellow').hide('fast');
});
.colored {
padding: 10px;
border: 1px solid #f00;
}
#red {
background: red;
}
#blue {
background: blue;
}
#green {
background: green;
}
#yellow {
background: yellow;
}
.s:focus {
border: 1px solid black;
}
.s:focus + yellow {
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="colored" id="red">
<form>
<input id="search" placeholder="Search" />
</form>
</div>
<div class="colored" id="blue">blue</div>
<div class="colored" id="green">green</div>
<div class="colored" id="yellow">yellow</div>
答案 6 :(得分:0)
在你的CSS中
.s:focus + yellow{
display: none;
}
这种风格有几个问题,首先是你错过的错误'。'在'黄色'类名之前进行选择。第二;由于'input.s'元素旁边没有'.yellow'元素,因此永远不会应用此样式。在CSS中尚未定义反向子到父选择器(还有!)。
var input = document.querySelectorAll(".s")[0];
input.addEventListener("focus", function () {
var yellow = document.querySelectorAll(".yellow")[0];
yellow.style.display = "none";
});
或jQuery方式:
$(".s").focus(function () {
$(".yellow").hide();//You can use .hide() or .fadeOut() or .slideUp() or .slideDown() methods.
});