在我的脚本中有三个 div。我想当我将鼠标悬停在第一行时使用
class="ab"
显示 div,并在将鼠标悬停在第二行时使用 class="abc"
显示 div。否则我想默认显示带有 class="a"
的 div。
但它永远不会显示带有
class="a"
的 div。
.abc,.ab {
display: none;
}
#f:hover ~ .ab {
display: block;
}
#f:hover ~ .abc,.a {
display: none;
}
#s:hover ~ .abc {
display: block;
}
#s:hover ~ .ab,.a {
display: none;
}
<a id="f">Show First content!</a>
<br/>
<a id="s">Show Second content!!</a>
<div class="a">Default Content</div>
<div class="ab">First content</div>
<div class="abc">Second content</div>
这是我的问题的 JSFiddle:JSFiddle 链接
要隐藏元素,请使用:
display: none;
visibility: hidden;
要显示元素,请使用:
display: block;
visibility: visible;
区别在于:
Visibility 处理标签的可见性,
display
处理它在页面上占用的空间。
如果设置了
visibility
并且不更改display
,即使看不到标签,仍然占用空间。
你需要
.abc,.ab {
display: none;
}
#f:hover ~ .ab {
display: block;
}
#s:hover ~ .abc {
display: block;
}
#s:hover ~ .a,
#f:hover ~ .a{
display: none;
}
更新了演示,位于http://jsfiddle.net/gaby/n5fzB/2/
原始CSS中的问题是CSS选择器中的
,
启动了一个全新的选择器。它没有组合..所以 #f:hover ~ .abc,.a
意味着 #f:hover ~ .abc
和 .a
。您将其设置为 display:none
,因此它始终设置为对所有 .a
元素隐藏。
您可以根据您的需求使用以下五种方式中的任何一种来隐藏元素。
.hide {
opacity: 0;
}
.hide {
visibility: hidden;
}
.hide {
display: none;
}
.hide {
position: absolute;
top: -9999px;
left: -9999px;
}
.hide {
clip-path: polygon(0px 0px,0px 0px,0px 0px,0px 0px);
}
要显示,请使用以下任意一项: 不透明度:1; 可见性:可见; 显示:块;
来源:https://www.sitepoint.com/ Five-ways-to-hide-elements-in-css/
HTML 代码:
<a id="f">Show First content!</a>
<br/>
<a id="s">Show Second content!!</a>
<div class="a">Default Content</div>
<div class="ab hideDiv">First content</div>
<div class="abc hideDiv">Second content</div>
脚本代码:
$(document).ready(function() {
$("#f").mouseover(function(){
$('.a,.abc').addClass('hideDiv');
$('.ab').removeClass('hideDiv');
}).mouseout(function() {
$('.a').removeClass('hideDiv');
$('.ab,.abc').addClass('hideDiv');
});
$("#s").mouseover(function(){
$('.a,.ab').addClass('hideDiv');
$('.abc').removeClass('hideDiv');
}).mouseout(function() {
$('.a').removeClass('hideDiv');
$('.ab,.abc').addClass('hideDiv');
});
});
CSS代码:
.hideDiv
{
display:none;
}
.abc,.ab {
display: none;
}
#f:hover ~ .ab {
display: block;
}
#f:hover ~ .abc,.a {
display: none;
}
#s:hover ~ .abc {
display: block;
}
#s:hover ~ .ab,.a {
display: none;
}
<a id="f">Show First content!</a>
<br/>
<a id="s">Show Second content!!</a>
<div class="a">Default Content</div>
<div class="ab">First content</div>
<div class="abc">Second content</div>
我有一个想要隐藏的类,这是我的代码:
/*--------------------------------------------------------------
Title & Breadcrumb
--------------------------------------------------------------*/
.main-title-section-wrapper {
clear: both;
float: left;
margin: 0;
padding: 75px 0 90px;
width: 100%;
}
#header-wrapper .main-title-section-wrapper {
position: relative;
}
#header-wrapper.header-top-absolute .main-title-section-wrapper {
position: static;
}
.main-title-section {
float: left;
width: 100%;
margin: 0;
padding: 0;
position: relative;
pointer-events: none;
}
.main-title-section h1 {
font-size: calc(var(--DTFontSize_H1)/.95);
margin: 0 0 5px;
word-break: break-all;
font-weight: 700;
text-transform: initial;
line-height: 1.25em;
color: #1a1a1a;
}
.breadcrumb {
clear: both;
float: left;
width: 100%;
margin: 0;
padding: 0;
font-size: inherit;
font-weight: 500;
}
.breadcrumb span:not(.current) {
display: inline-block;
margin: 0px 12px;
padding: 0;
}
.main-title-section-wrapper .breadcrumb-default-delimiter:before {
content: "";
background-color: currentColor;
display: inline-block;
height: 14px;
opacity: 0.45;
position: relative;
top: -1px;
vertical-align: middle;
width: 1px;
-webkit-transform: rotate(20deg);
transform: rotate(20deg);
}
.main-title-section h1,
.breadcrumb {
hyphens: auto;
word-break: break-word;
word-wrap: break-word;
-moz-hyphens: auto;
-webkit-hyphens: auto;
-ms-hyphens: auto;
}
.main-title-section-wrapper>.main-title-section-bg,
.main-title-section-wrapper>.main-title-section-bg:after {
content: "";
height: 100% !important;
overflow: hidden;
position: absolute;
top: 0;
left: 0;
width: 100%;
z-index: -1;
pointer-events: none;
}
/*--------------------------------------------------------------
Default Colors
--------------------------------------------------------------*/
.breadcrumb {
color: var(--DTBodyTxtColor);
}
.main-title-section h1 {
color: var(--DTBlackColor);
}
.breadcrumb a {
color: var(--DTLinkColor);
border-bottom: 2px solid transparent;
}
.breadcrumb a:hover {
border-bottom-color: var(--DTPrimaryColor);
}
.breadcrumb span.current {
color: var(--DTBodyTxtColor);
}
.main-title-section-wrapper>.main-title-section-bg:after {
background-color: rgba(var(--DTBlack_RGB), 0.05);
}
.main-title-section-wrapper.dark-bg-breadcrumb>.main-title-section-bg {
background-color: var(--DTBlackColor);
}
.dark-bg-breadcrumb .main-title-section h1,
.dark-bg-breadcrumb .breadcrumb a,
.dark-bg-breadcrumb .breadcrumb span.current,
.dark-bg-breadcrumb .breadcrumb span:not(.current) {
color: var(--DTWhiteColor);
}
.breadcrumb a:hover,
.dark-bg-breadcrumb .breadcrumb a:hover {
color: var(--DTLinkHoverColor);
}
/*--------------------------------------------------------------
Accents
--------------------------------------------------------------*/
/* Primary Color */
.breadcrumb a:hover {
color: var(--DTPrimaryColor);
}
/*--------------------------------------------------------------
Responsive
--------------------------------------------------------------*/
@media only screen and (min-width:1281px) {
.main-title-section-wrapper {
padding: 75px 0 90px;
}
}
@media only screen and (max-width: 1280px) {
.main-title-section-wrapper {
padding: 45px 0 50px;
}
}
/*----*****---- << Mobile (Landscape) >> ----*****----*/
/* Common Styles for the devices below 767px width */
@media only screen and (max-width: 767px) {
.main-title-section h1 {
font-size: 28px;
}
.main-title-section,
.main-title-section h1,
.breadcrumb {
text-align: center;
}
}
/* Common Styles for the devices below 479px width */
@media only screen and (max-width: 479px) {
.main-title-section h1 {
font-size: 24px;
}
}
I'm trying to hide main title section wrapper, so far I used this:
.main-title-section-wrapper {
display: none ;
visibility: hidden ;
}
.main-title-section-wrapper {
position: absolute;
top: -9999px;
left: -9999px; }
它可以在桌面版和移动版的 chrome 上运行,但 ios 上的 safari 仍然显示该类
我应该怎么做才能在 safari 中隐藏这个元素?
html代码:
<button class="Show">Show</button>
<button class="Hide">Hide</button>
<button class="toggle">Show & Hide</button>
<div id="target"></div>
CSS代码:
#target {
background:#0099cc;
width:300px;
height:300px;
height:160px;
padding:5px;
display:none;
}
.Hide
{
display:none;
}
javascript代码:
$('.Show').click(function() {
$('#target').show(200);
$('.Show').hide(0);
$('.Hide').show(0);
});
$('.Hide').click(function() {
$('#target').hide(500);
$('.Show').show(0);
$('.Hide').hide(0);
});
$('.toggle').click(function() {
$('#target').toggle('slow');
});