gpt4 book ai didi

javascript - jQuery - 查找最后选择的元素

转载 作者:行者123 更新时间:2023-11-30 09:30:23 27 4
gpt4 key购买 nike

我正在从 Head First 系列学习 jQuery 并处理其中一个示例,我需要找到最后选择的元素。

这是我从第 2 章中得到的:该页面包含四张图片,当用户点击它们时,会向他们提供一条随机折扣消息,该消息出现在每张图片的底部。为了使事情看起来更有趣,我将 slideUp() 函数添加到我从同一本书的第 1 章中学到的消息中。

这是我的进展:当用户点击其中一张图片时,随机折扣消息会在其下方滑动以显示其消息。当用户单击任何其他图像时,较早的消息会向上滑动,并且新的随机折扣消息会向下滑动到单击的图像下方。这是我的代码的简化版本。

$('.jumpDiv').click(function () {
$('.jumpDiv').children('.discountDiv').slideUp();
$('.jumpDiv p').remove();
var discount = Math.floor((Math.random() * 5) + 5);
var msg = '<p>Your discount is ' + discount + '%</p>';
$(this).children('.discountDiv').append(msg);
$(this).children('.discountDiv').slideDown();
});
.jumpDiv{
float:left;
}
#main{
border:1px solid;
height:500px;
width:auto;
background-color: grey;
}
#main .jumpDiv{
border-right: 1px solid;
border-bottom: 1px solid;
height:245px;
width:245px;
font-size:20px;
}
#main .jumpDiv>div{
text-align:center;
background-color:#fee;
cursor: pointer;

}
.discountDiv{
text-align: center;
display:none;
border:1px solid;
border-bottom-left-radius: 10px;
border-bottom-right-radius: 10px;

}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

<body>
<div id="main">
<div class="jumpDiv">
<div> Click Here</div>
<div class="discountDiv">

</div>
</div>
<div class="jumpDiv">
<div> Click Here</div>
<div class="discountDiv">

</div>
</div>
<div class="jumpDiv">
<div> Click Here</div>
<div class="discountDiv">

</div>
</div>
<div class="jumpDiv">
<div> Click Here</div>
<div class="discountDiv">

</div>
</div>
</div>
<script
src="https://code.jquery.com/jquery-3.2.1.js"
integrity="sha256-DZAnKJ/6XZ9si04Hgrsxu/8s717jcIzLy3oi35EouyE="
crossorigin="anonymous"></script>
</body>

我需要解决的问题是,如果用户重复点击同一个 div,折扣消息 div 应该保持原样,只更新随机生成的内容。如果用户再次单击同一个 div,如何阻止此 div 上下滑动。有没有办法访问最后单击的元素并以某种方式停止折扣消息在更新消息之前向上滑动。

最佳答案

您必须存储对 lastClicked 实体的引用并将其与下一个单击值进行比较,如果相同则不执行任何操作。请看以下内容。

var lastClicked = null;
$('.jumpDiv').click(function () {
if(lastClicked === this) {
/*Don't do anything*/
return;
}

$('.jumpDiv').children('.discountDiv').slideUp();
$('.jumpDiv p').remove();
var discount = Math.floor((Math.random() * 5) + 5);
var msg = '<p>Your discount is ' + discount + '%</p>';
$(this).children('.discountDiv').append(msg);
$(this).children('.discountDiv').slideDown();

lastClicked = this;
});
.jumpDiv{
float:left;
}
#main{
border:1px solid;
height:500px;
width:auto;
background-color: grey;
}
#main .jumpDiv{
border-right: 1px solid;
border-bottom: 1px solid;
height:245px;
width:245px;
font-size:20px;
}
#main .jumpDiv>div{
text-align:center;
background-color:#fee;
cursor: pointer;

}
.discountDiv{
text-align: center;
display:none;
border:1px solid;
border-bottom-left-radius: 10px;
border-bottom-right-radius: 10px;

}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

<body>
<div id="main">
<div class="jumpDiv">
<div> Click Here</div>
<div class="discountDiv">

</div>
</div>
<div class="jumpDiv">
<div> Click Here</div>
<div class="discountDiv">

</div>
</div>
<div class="jumpDiv">
<div> Click Here</div>
<div class="discountDiv">

</div>
</div>
<div class="jumpDiv">
<div> Click Here</div>
<div class="discountDiv">

</div>
</div>
</div>
<script
src="https://code.jquery.com/jquery-3.2.1.js"
integrity="sha256-DZAnKJ/6XZ9si04Hgrsxu/8s717jcIzLy3oi35EouyE="
crossorigin="anonymous"></script>
</body>

关于javascript - jQuery - 查找最后选择的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46645645/

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