因此,本质上我想要一个元素,当点击时,触发一个jQuery脚本,添加或删除类(在这种情况下,我添加一个将background-image css属性更改为只有该特定元素的类)。jQuery脚本添加/删除类成功添加类,但不能删除相同的类
function readMore() {
var subID = event.target.id;
var footerTarget = $('[id='+subID+'][class="articleFooter"]');
var newTarget = $('[id='+subID+'][class="showArticlePara"]');
newTarget.toggle();
var footerTarget = $('[id='+subID+'][class="articleFooter"]');
if (newTarget.css("display") == "block") {
footerTarget.addClass("changeBackgroundImage");
}
else {
footerTarget.removeClass("changeBackgroundImage");
}
alert(footerTarget.class());
}
$(document).ready(function() {
});
.articleSection {
\t width: 100%;
\t display: block;
\t font-family: Trebuchet MS;
\t font-size: 1.1em;
\t color: white;
\t margin-bottom: 25px;
\t padding-bottom: 3px;
\t background-color: RGBA(255, 255, 255, 0.1);
\t box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
\t border-radius: 2px;
\t box-sizing:border-box;
-moz-box-sizing:border-box;
-webkit-box-sizing:border-box;
-ms-box-sizing:border-box;
}
.articleContent {
\t /*height: 70px;*/
\t padding: 10px 15px 5px 15px;
\t box-sizing:border-box;
-moz-box-sizing:border-box;
-webkit-box-sizing:border-box;
-ms-box-sizing:border-box;
}
.articleVotes {
}
.voteBox {
}
.articleFooter {
\t width: 100%;
\t height: 10px;
\t content: 'more';
\t background-image:url('../Images/Icons/showMoreBlack13030.png');
\t background-size: contain;
\t background-repeat: no-repeat;
\t background-position: center center;
\t transition: 0.2s ease-in-out;
}
.articleFooter:hover {
\t background-image: url('../Images/Icons/chevron13040Blue.png');
}
.changeBackgroundImage {
\t width: 100%;
\t height: 10px;
\t content: 'less';
\t background-image:url('../Images/Icons/showLessBlack13030.png');
\t background-size: contain;
\t background-repeat: no-repeat;
\t background-position: 15px center;
\t transition: 0.2s ease-in-out;
}
.changeBackgroundImage:hover {
\t background-image: url('../Images/Icons/chevron13040BlueRotated.png');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="articleSection">
<div class="articleContent">
<h2>Exciting new study shows that in any male group, at least one is gay</h2>
<div class="showArticlePara" id="one">
<p>
I really hope it's Luke, he's cute af.
</p>
</div>
</div>
<div class="articleVotes">
<div class="voteBox"></div>
</div>
<div class="articleFooter" id="one" onclick="readMore()"></div>
</div>
所以当我点击.articleFooter
与id=subID
我已经与它改变使用简单的jQuery toggle()
显示属性相关联的段落的显示。我尝试过使用toggleClass()
,但这只会添加该类,并不会在第二次点击时将其删除。
我终于尝试的if/else
声明,检查如果先前影响款display: none
和添加或删除基于结果的类,但再次这只是成功添加类,但未能在除去同一类第二次点击。
感谢您的任何建议和/或帮助。
的jsfiddle:https://jsfiddle.net/hm3y3848/
对多个元素使用相同的id'one'。 ID意味着独特。 – Titus
@Titus我知道,我知道,糟糕的礼仪和所有这些,但当时似乎是一个好主意,我希望它不会影响我目前的问题,因此本质上;一步一步来。 –
@PaulMcGlinchey - 这不仅仅是坏礼节,它实际上影响了JS和jQuery的功能。 。 。例如,选择(在JS和jQuery中)多次在DOM中的id,将只返回具有该id的第一个元素。 – talemyn