简体   繁体   English

HTML CSS类选择器

[英]HTML CSS Class Selector

I need help on understanding the class selector code. 我需要了解类选择器代码的帮助。 Currently I have these: 目前,我有这些:

<div class="tt-news">
    <div class="tt-news-top">
        <a class="tt-news-img custom-hover" href="http://www.cfbtvideos.org/cfbt/2017/10/11/puppet-workshop/"><img alt="" class="img-responsive wp-post-image" height="168" sizes="(max-width: 360px) 100vw, 360px" src="http://www.cfbtvideos.org/cfbt/wp-content/uploads/2017/10/Article-2.jpg" srcset="http://www.cfbtvideos.org/cfbt/wp-content/uploads/2017/10/Article-2.jpg 750w, http://www.cfbtvideos.org/cfbt/wp-content/uploads/2017/10/Article-2-300x140.jpg 300w" width="360"></a>
        <div class="tt-news-date" style="display: block;">
            <span>11</span> Oct
        </div>
    </div>
    <div class="tt-news-label">
        <span>By: <a>cfbt admin</a></span>
    </div><a class="tt-news-title c-h6" href="http://www.cfbtvideos.org/cfbt/2017/10/11/puppet-workshop/" style="display: block;">Puppet Workshop</a>
    <div class="simple-text size-4">
        &nbsp; An optional PD session entitled “Making a Bruneian Student Finger Puppet” was held on Wednesday ...
    </div>

Im trying to show the .tt-news-date span and .tt-news-label .tt-news-title on hovering the .custom-hover 我试图在悬停.custom-hover时显示.tt-news-date跨度.tt-news-label .tt-news-title

Right now, I have this code: 现在,我有以下代码:

jQuery(document).ready( function( $ ) {
var posthover = $(".custom-hover.custom-hover");

$(posthover).hover( function() {
$(".tt-news-date").css("display", "block");
$(".tt-news-title").css("display", "block");
});

});

But this will show all of the title and date on hovered. 但这会显示所有标题和日期。 I just need to show the closest to the one's I hovered. 我只需要显示最接近我所徘徊的那个。 How do I do this? 我该怎么做呢?

Why use jQuery for something that can be done solely with CSS. 为什么将jQuery用于只能通过CSS完成的事情。

The code below will only show the sections .tt-news-date span and .tt-news-title when you hover over the section .tt-news. 当您将鼠标悬停在.tt-news部分上时,下面的代码将仅显示.tt-news-date span和.tt-news-title部分。

I know it isn't exactly what you want, but it should work close enough that you no longer need JavaScript to do it. 我知道这并不是您想要的,但它应该足够接近以至于您不再需要JavaScript即可。

 .tt-news:not(:hover) .tt-news-date, .tt-news:not(:hover) .tt-news-title { display: none; } 
 <div class="tt-news"> <div class="tt-news-top"> <a class="tt-news-img custom-hover" href="http://www.cfbtvideos.org/cfbt/2017/10/11/puppet-workshop/"><img alt="" class="img-responsive wp-post-image" height="168" sizes="(max-width: 360px) 100vw, 360px" src="http://www.cfbtvideos.org/cfbt/wp-content/uploads/2017/10/Article-2.jpg" srcset="http://www.cfbtvideos.org/cfbt/wp-content/uploads/2017/10/Article-2.jpg 750w, http://www.cfbtvideos.org/cfbt/wp-content/uploads/2017/10/Article-2-300x140.jpg 300w" width="360"></a> <div class="tt-news-date"> <span>11</span> Oct </div> </div> <div class="tt-news-label"> <span>By: <a>cfbt admin</a></span> </div> <a class="tt-news-title c-h6" href="http://www.cfbtvideos.org/cfbt/2017/10/11/puppet-workshop/">Puppet Workshop</a> <div class="simple-text size-4"> &nbsp; An optional PD session entitled “Making a Bruneian Student Finger Puppet” was held on Wednesday ... </div> </div> 

UPDATE 更新

If you wanted to do it with jQuery you can simplify your code as follows: 如果您想使用jQuery,可以按以下方式简化代码:

 $(document).ready( function() { $('.custom-hover').mouseenter( function() { $('.tt-news-date,.tt-news-title').css('display', 'block'); } ).mouseleave( function() { $('.tt-news-date,.tt-news-title').css('display', ''); } ); } ); 
 .tt-news-date, .tt-news-title { display: none; } .tt-news-img img { background-color: #444; min-height: 20px; min-width: 20px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="tt-news"> <div class="tt-news-top"> <a class="tt-news-img custom-hover" href="http://www.cfbtvideos.org/cfbt/2017/10/11/puppet-workshop/"><img alt="" class="img-responsive wp-post-image" height="168" sizes="(max-width: 360px) 100vw, 360px" src="http://www.cfbtvideos.org/cfbt/wp-content/uploads/2017/10/Article-2.jpg" srcset="http://www.cfbtvideos.org/cfbt/wp-content/uploads/2017/10/Article-2.jpg 750w, http://www.cfbtvideos.org/cfbt/wp-content/uploads/2017/10/Article-2-300x140.jpg 300w" width="360"></a> <div class="tt-news-date"> <span>11</span> Oct </div> </div> <div class="tt-news-label"> <span>By: <a>cfbt admin</a></span> </div> <a class="tt-news-title c-h6" href="http://www.cfbtvideos.org/cfbt/2017/10/11/puppet-workshop/">Puppet Workshop</a> <div class="simple-text size-4"> &nbsp; An optional PD session entitled “Making a Bruneian Student Finger Puppet” was held on Wednesday ... </div> </div> 

Make sure to handle both mouseenter and mouseleave to show and hide the special data. 确保同时处理mouseentermouseleave以显示和隐藏特殊数据。

The image was not loading for me so I needed to set its width and height so I had something to hover over. 图片没有为我加载,因此我需要设置其宽度和高度,以便将鼠标悬停在上面。 The CSS related to that is strictly for testing. 与此相关的CSS严格用于测试。

hello as @intervalia said is better with pure css, but to do it in JQuery in your question you have some issues, the item have a inline style 'style="display: block;"' maybe you intention is these elements where not visible so add a class for make them 'display: none', and your selector is wrong change var posthover = $(".custom-hover.custom-hover"); 你好,如@intervalia所说,使用纯css更好,但是要在您的问题中的JQuery中执行此操作,您会遇到一些问题,该项目具有内联样式'style =“ display:block;”'也许您的目的是这些元素不可见因此,添加一个使它们“显示:无”的类,并且选择器错误,请更改var posthover = $(“。custom-hover.custom-hover”); for var posthover = $(".custom-hover"); 对于var posthover = $(“。custom-hover”);

$(function() {
    var posthover = $(".custom-hover");

    $(posthover).hover( function() {
        $(".tt-news-date").css("display", "block");
        $(".tt-news-title").css("display", "block");
    });
});

example

hope it helps. 希望能帮助到你。

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

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