gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 12:09:17 25 4
gpt4 key购买 nike

我有一个 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 fiddle ,但它在那里也不起作用,所以我不知道它在旧网站上是如何工作的!

行:

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

总是返回 false,即使在 .hr-banner-text 的 CSS 中设置了 display:none当我尝试手动将 var shouldShow 设置为 true 时,hide() 和 show() 函数仍然不起作用。我认为语法错误并且“this”不正确但不知道如何修复。

谁能帮我解决这个问题?我不太熟悉 JQuery 语法。

最佳答案

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

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

您需要使用 siblings() .

此外,我建议使用 .is(':visible')is(' :hidden') 检查元素的可见性。

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

查看 fiddle 中的注释代码,您正在尝试根据元素的可见性切换元素。您可以使用 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>

关于JQuery 语法问题 - 使用 'this' 并检查 CSS 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35154504/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com