简体   繁体   English

在Jquery中应用CSS属性

[英]Applying CSS property in Jquery

I am working on 'OnClick' functionality in jQuery. 我正在使用jQuery中的“ OnClick”功能。 When we click Div property other divs should fade out. 当我们单击Div属性时,其他div应该淡出。

Please find the code below. 请在下面找到代码。 HTML: HTML:

<div>1</div>
<div class="ree">2</div>
<div class="foo">
  <div class = "bar">
     <div class="nike">3</div>
  </div>
</div>
<div>4</div>

jQuery: jQuery的:

$('.nike').on('click',function ()
              {
$('div:not(.nike)').css('opacity','0.2')

              });

Here When I click on Div Class 'Nike', Except 'Nike' all Div classes should fade out. 在这里,当我单击Div类“ Nike”时,除“ Nike”外,所有Div类都应淡出。 But here All Divs including Nike is fading out. 但是,包括耐克在内的所有Divs都在逐渐消失。

http://jsfiddle.net/6V8hr/5/ http://jsfiddle.net/6V8hr/5/

Thanks all 谢谢大家

Since you have nested DIVs, those parent DIVs are getting faded out, also causing your nike div to fade out. 由于您嵌套了DIV,因此这些父DIV逐渐淡出,这也导致nike div淡出。

While this code isn't perfect... it works for what you need it to. 虽然这段代码并不完美……但是它可以满足您的需要。

DEMO DEMO

$('.nike').on('click',function () {
   $('div').not('.foo').not('.bar').not('.nike').css('opacity','0.2')
});

So I'm basically listed the classes in your tree containing nike, making sure none of those are affected. 因此,我基本上在您的树中列出了包含耐克的类,请确保所有这些都不受影响。

I read the HTML all 10 kinds of wrong. 我读了HTML的所有10种错误。 Below is the revised, short sweet and to the point. 以下是经过修订的简短说明。 Add class="hideThis" to any divs you would want to be "hidden". class="hideThis"添加到要“隐藏”的所有div中。 If you have multiple sibling divs that you want to hide/show on click, you could give them all the hideThis class and replace the $('.nike') with $('.hideThis') 如果您要单击时隐藏/显示多个同级div,则可以给它们全部hideThis类,然后用$('.hideThis')替换$('.nike') $('.hideThis')

$('.nike').click(function() {
  $(this).css('opacity','1'); //in case something else got clicked & hid this
  $('.hideThis').not($(this)).css('opacity','0.2'); //hide everything else
}
$('.nike').on('click', function() {
    $('div:not(.nike):not(:has(.nike))').css('opacity', '0.2');
});

Well this code seems better suited 那么这段代码似乎更适合

$('.nike').on('click', function () {
  $('div').each(function () {
    if ($('.nike').closest($(this)).length == 0) {
        $(this).css('opacity', '0.2');
    }
  })
});

http://jsfiddle.net/H17737/nr5bL/ http://jsfiddle.net/H17737/nr5bL/

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

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