JQuery语法问题 - 使用' this'并检查CSS属性

时间:2016-02-02 13:12:12

标签: jquery css

我有一个Jquery功能,可以在旧网站上正常工作。当将一些内容移动到新网站时,该功能不再有效,我无法弄清楚为什么会这样。

 <div class="hr-banner noselect"><img draggable="false" src="/wp-content/uploads/2015/11/people-development.jpg" alt="" /></div>
    <p class="hr-banner-title">People Development</p>

    <div class="hr-banner-text">

    As an employer, we understand the value of investing in the skills and knowledge of our employees, allowing

    them to develop and achieve their own professional goals.

    </div>
<div class="hr-banner noselect"><img draggable="false" src="/wp-content/uploads/2015/11/people-development.jpg" alt="" /></div>
    <p class="hr-banner-title">People Development</p>

    <div class="hr-banner-text">

    As an employer, we understand the value of investing in the skills and knowledge of our employees, allowing

    them to develop and achieve their own professional goals.

    </div>


$(".hr-banner").click(function(evt) {

    var shouldShow = $(".hr-banner-text", this).css("display") == "none";
    console.log(shouldShow);
        $(".hr-banner-title", this).hide();
        $(".hr-banner-text", this).show();
    if (shouldShow) {
        $(".hr-banner-title", this).hide();
        $(".hr-banner-text", this).show();
        console.log("should show");
    } else {
        $(".hr-banner-title", this).show();
        $(".hr-banner-text", this).hide();
        console.log("no show");
    }
});

我已经设置了一个JS小提琴,它也没有在那里工作,所以我不知道它是如何在旧网站上工作的!

该行:

var shouldShow = $(".hr-banner-text", this).css("display") == "none";

始终返回false,即使在 .hr-banner-text 的CSS中设置了 display:none 当我尝试手动将 var shouldShow 设置为true时,hide()和show()函数仍然无效。我认为语法是错误的,而且这个&#39;我没有改正,但不知道如何解决。

任何人都可以帮我解决这个问题吗?我对JQuery语法不太满意。

1 个答案:

答案 0 :(得分:1)

$(".hr-banner-text", this)将在当前( this )元素中搜索元素.hr-banner-text

因为,元素.hr-banner-text没有嵌套在.hr-banner元素中,所以不会选择任何元素。

您需要使用siblings()

此外,我建议您使用.css('display').is(':visible')来检查元素的可见性,而不是is(':hidden')

var shouldShow = $(this).siblings('.hr-banner-text').is(":hidden");

查看小提琴中的注释代码,您尝试根据元素的可见性切换元素。您可以使用toggle()

将元素包装在.hr-banner

之后

Demo

$(".hr-banner").click(function() {
    $(".hr-banner-title, .hr-banner-text").toggle();
});
.hr-banner {
    padding: 0;
    margin-bottom: 1em;
    cursor: pointer;
}

.hr-banner > img {
    width: 98%;
}

.hr-banner-title {
    margin: 0;
    color: #FFF;
    line-height: 2em;
    margin-top: -3em;
    margin-left: 16px;
    font-size: 24px;
    font-weight: bold;
}

.hr-banner-text {
    display: none;
    position: absolute;
    top: 0;
    padding: 16px;
    color: #FFF;
    font-size: 1.2em;
    background-color: rgba(0, 0, 0, 0.6);
    width: 98%;
    height: 100%;
}

.hr-noselect {
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<div class="hr-banner noselect"><img draggable="false" src="https://daisygroup.com/wp-content/uploads/2015/11/people-development.jpg" alt="" />
    <p class="hr-banner-title">People Development</p>

    <div class="hr-banner-text">

        As an employer, we understand the value of investing in the skills and knowledge of our employees, allowing them to develop and achieve their own professional goals.

    </div>
</div>