简体   繁体   English

用jQuery和CSS隐藏元素之间有什么区别?

[英]What is the difference between hiding elements with jQuery and CSS?

I created something similar to slider, which has three blocks with two pictures and description in each. 我创建了类似于滑块的内容,其中包含三个带有两个图片和说明的块。 At the top it shows one of these blocks with its two pictures and description. 它在顶部显示了其中两个图块及其说明之一。 At the bottom you can see previews for two other blocks without description. 在底部,您可以看到其他两个块的预览而没有描述。 It looks like this (pictures are random): 看起来像这样(图片是随机的):

http://plnkr.co/edit/o9wTYhrTnpfgXu4aEIYW?p=preview http://plnkr.co/edit/o9wTYhrTnpfgXu4aEIYW?p=preview

Here it works as expected, but there is one thing I don't understand. 在这里它按预期工作,但是有一件事我不明白。 When you click at the bottom preview, it removes description from top active pictures, and adds description to bottom inactive ones, and blocks change position. 当您单击底部预览时,它会从顶部活动图片中删除描述,并向底部不活动的图片中添加描述,并阻止更改位置。 By default all the figcaptions with descriptions have class .sliderblock__caption and are hidden in css, but those with class .sliderblock__caption--active are displayed: 默认情况下,所有带有说明的figcaptions都具有.sliderblock__caption类,并且隐藏在css中,但是将显示具有class .sliderblock__caption--active的类。

.sliderblock__caption{
    display: none;
}

.sliderblock__caption--active{
    display: block;
}

With css it works fine, but when I try to us js instead, it doesn't. 使用css可以正常工作,但是当我尝试使用js时,效果却并非如此。

http://plnkr.co/edit/kpnx9oGJzvz94u7rjGTM?p=preview http://plnkr.co/edit/kpnx9oGJzvz94u7rjGTM?p=预览

I placed the same hide stuff in js-file, but it's not hiding what is expected to be hidden. 我在js文件中放置了相同的隐藏内容,但它没有隐藏预期要隐藏的内容。

var caption = ".sliderblock__caption";
var activecaption = ".sliderblock__caption--active";

$(caption).css("display", "none");
$(activecaption).css("display", "block");

If you click on one of the bottom blocks, the block will change position, but descriptions will not. 如果单击底部的块之一,则该块将更改位置,但说明不会更改。

The point is that it actually adds the "active" class to figcaptions, but it's not working. 关键是它实际上向“ figcaptions”添加了“活动”类,但是没有用。 When you look at the elements in Firebug, it shows that figcaptions at the top have "active" class, but their css is following: 当您查看Firebug中的元素时,它表明顶部的figcaptions具有“活动”类,但是它们的css在下面:

element.style{
    display: none;
}

Where does this rule come from? 这个规则从何而来? And where should I put the piece of code in js to get the same effect as with css? 而且我应该将代码片段放在js中以获得与CSS相同的效果?

When you do $myElm.hide() or $myElm.css("display", "none") you're setting the style attribute to display:none . 当您执行$myElm.hide()$myElm.css("display", "none")您会将style属性设置为display:none That's the inline element CSS. 那就是内联元素CSS。

You can do the same using the style attribute: 您可以使用style属性执行相同的操作:

<div style="display:none">

Using CSS (even you're using inline or rules in your file), doesn't require JavaScript while jQuery css method require the browser to allow executing of JavaScript. 使用CSS(即使您在文件中使用内联或规则)也不需要JavaScript,而jQuery css方法要求浏览器允许执行JavaScript。

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

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