2017-08-08 103 views
2

这里是我的代码:为什么.not()没有任何影响?

$('body').not('.specific_element').css('opacity','.3');
body{ 
 
    border: 1px solid; 
 
} 
 
.specific_element{ 
 
    border: 1px solid red; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<p>There are some texts</p> 
 
<div class="specific_element"> 
 
    sth 
 
</div> 
 
<p>whatever</p>

所有我想要做的就是保证它们有一个红色的边框1元素的不透明度。我的意思是我想在除特定元素之外的所有内容上应用opacity: .3。我怎样才能做到这一点?

+0

特定元素为人体内部,你对身体施加的不透明度。所以即使排除一个元素,身体内部的所有元素都将不透明,因为该元素仍在体内 – guradio

+0

@guradio那里'.not()'的工作是什么? – stack

+0

因此,即使您排除一个元素,因为该元素仍在体内,所有身体内部的所有内容都将具有不透明性 – guradio

回答

3
  1. 应用不透明的容器的儿童与特定元素之外

$('body').children().not('.specific_element').css('opacity','.3');
body{ 
 
    border: 1px solid; 
 
} 
 

 
.specific_element{ 
 
    border: 1px solid red; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<p>There are some texts</p> 
 
<div class="specific_element"> 
 
    sth 
 
</div> 
 
<p>whatever</p>

0

由于@guradio建议,将通过上应用不透明度来实现兄弟姐妹的具体元素:

$('.specific_element').siblings().css('opacity','.3');
body{ 
 
    border: 1px solid; 
 
} 
 

 
.specific_element{ 
 
    border: 1px solid red; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<p>There are some texts</p> 
 
<div class="specific_element"> 
 
    sth 
 
</div> 
 
<p>whatever</p>

2

// Select all elements inside body except '.specific_element' 
$('body *').not('.specific_element').css('opacity','.3'); 
+0

我建议'$('body> *')' - 因为否则,有效不透明度下的两个级别将是0.09,三级下降0.027,然后是0.0081等 –

1
$('*:not(*[class*="specific_element"])').css('opacity','.3'); 

一切除了特定元素。

使用*选择一切

相关问题