jquery - 使用 jQuery 添加类不显示效果

标签 jquery html css class addclass

我有一个 HTML 如下:

<div class="container">
      <!-- Example row of columns -->
      <div class="row">
        <div class="span7">
          <div class="home-image left-image">
            <img class="photography-left-top" src="http://placehold.it/700x440" />
            <p class="image-text_left">Wedditng Photography</p>
          </div>

          <h5>Heading</h5>
        </div>
        <div class="span5">
            <div class="home-image right-image right-image-top">
                <img class="photography-right-top" src="http://placehold.it/400x200" />
                <p class="image-text_right-top">Fashion Photography</p>
          </div>
          <div class="home-image right-image right-image-bottom">
            <img class="photography-right-bottom" src="http://placehold.it/400x200" />
            <p class="image-text_right-bottom">Contact</p>
          </div>

          <h5>Heading</h5>
       </div>

      </div>

 </div>        

还有 CSS:

.home-image {
    position:relative;
}
.photography-left-top,.photography-right-top,.photography-right-bottom {
    position : absolute;
}
.image-text_left {
    display:none;
    position:absolute;
    width:300px;
    top:200px;;
    left:200px;
    z-index : 100;
    font-weight:bold;
}
.image-text_right-top,.image-text_right-bottom {
    display:none;
    position:absolute;
    width:300px;
    top:100px;
    left:100px;
    z-index : 100;
    font-weight:bold
}
.right-image-bottom {
    margin-top:220px;
    position:relative
}
.right-top {
    position:relative
}
.home-img-hover {
    background:#911717;
}

JS:

$(document).ready(function(){

        $(".left-image").hover(function () {
            $(this).find('.photography-left-top').fadeTo('slow',0);
            $(this).addClass('home-img-hover');
            $('.image-text_left').show();
        },
        function () {
            $(this).find('.photography-left-top').fadeTo('400',1.0);
            $('.image-text_left').hide();
        });

因为 addClass('home-img-hover') 会将 a 类添加到当前 class 我期待在我将鼠标悬停在 left 上时出现背景颜色-图像。但它显示白色默认背景。为什么?

Sample in JSFiddle

最佳答案

它完全符合您的预期,您只是看不到它,因为 .home-image 没有高度,因为它里面的所有内容都是从普通文档中取出的流(使用 position:absolute)。尝试给 .home-image 一些尺寸,您就会明白我的意思:

.home-image {
    position:relative;
    width:400px;
    height:240px;
}

JSFiddle

关于jquery - 使用 jQuery 添加类不显示效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20609503/

相关文章:

jquery - JQuery 选择器中的 'span :first' 和 'span:first' 之间有区别吗?

javascript - Jquery Mobile Listview 自动完成 : how to trigger function when user presses enter?

jQuery:将内容添加到div末尾

css - 为什么位置:absolute image enables scroll bar?

javascript - 如何将 div 元素放入 jQuery Slider 中,显示至少 5 个元素,并使用左右 slider 查看更多元素?

javascript - jquery单击时更改图像src,并在第二次单击时恢复为原始图像

javascript - CSS 向下滚动动画处理轨迹线

javascript - Jquery中如何使用fancybox

html - IE7 ul 中的奇怪间距

html - 转换在 anchor 上无法正常工作