gpt4 book ai didi

jquery - 如何在 jQuery 中基于 $(this) 选择器选择一个 div?

转载 作者:行者123 更新时间:2023-11-28 16:02:43 25 4
gpt4 key购买 nike

我的代码看起来像这样:

$(document).ready(function(){
"use strict";
$(".box_button").click(function(){
var content_front = $(this).closest('.front');
var content_back = $(this).parents('.box_content');

content_front.addClass("flip_1");
content_back.addClass("flip_2");
});
});
body {
background: #f4f4f4;
}

.col_x4 {
position: relative;
display: block;
float: left;
width: 300px;
height: auto;
box-sizing: border-box;
padding: 7px;
}

.box {
position: relative;
display: block;
box-sizing: border-box;
width: 100%;
border-radius: 4px;
perspective: 1000px;
}

.box_top {
position: relative;
display: block;
box-sizing: border-box;
width: 100%;
overflow: hidden;
border-radius: 4px 4px 0 0;
border: 1px solid #e3e3e3;
border-bottom: hidden;
}

.box_top img {
position: relative;
display: block;
max-width: 100%;
height: auto;
-webkit-transition: all 0.3s ease-in-out;
transition: all 0.3s ease-in-out;
}

.box_top img:hover {
-webkit-transform: scale(1.2,1.2);
transform: scale(1.2,1.2);
}

.box_content {
position: relative;
display: block;
box-sizing: border-box;
transform-style: preserve-3d;
}

.front {
position: absolute;
display: block;
box-sizing: border-box;
top: 0; left: 0;
width: 100%;
height: auto;
background-color: #fff;
z-index: 2;
transform: rotateY(0deg);
transition: all 0.6s ease-in-out;
border: 1px solid #e3e3e3;
min-height: 281px;
}

.back {
position: absolute;
display: block;
box-sizing: border-box;
top: 0; left: 0;
width: 100%;
height: auto;
z-index: 1;
background-color: #fff;
transform: rotateY(180deg);
transition: all 0.6s ease-in-out;
backface-visibility: hidden;
border: 1px solid #e3e3e3;
min-height: 281px;
}

.flip_1 {
z-index: 1;
transform: rotateY(180deg);
backface-visibility: hidden;
}

.flip_2 {
z-index: 2;
transform: rotateY(360deg);
backface-visibility: visible;
}

.box_title {
position: relative;
margin: 10px 0;
padding: 0;
text-align: center;
}

.t1::after,
.t2::after {
content: "";
position: relative;
display: block;
width: 100%;
height: 20px;
margin: 10px 0 0;
background: url(https://pixabay.com/static/uploads/photo/2012/05/07/02/47/green-47700_960_720.png) no-repeat center center;
background-size: contain;
}

.t2::before {
content: "";
position: relative;
display: block;
width: 100%;
height: 90px;
background: url(../images/basket-full.png) no-repeat center center;
background-size: contain;
margin: 25px 0 5px;
}

.box_description {
position: relative;
display: block;
box-sizing: border-box;
width: 90%;
margin: 0 5% 15px;
padding: 0 15px;
text-align: center;
line-height: 20px;
max-height: 100px;
overflow: auto;
}

.box_description::-webkit-scrollbar
{
width: 4px; /* for vertical scrollbars */
height: 4px; /* for horizontal scrollbars */
}

.box_description::-webkit-scrollbar-track
{
background: #e3e3e3;
border-radius: 2px;
}

.box_description::-webkit-scrollbar-thumb
{
background: rgba(61,82,95,0.3);
border-radius: 2px;
}

.box_price {
position: relative;
display: block;
width: 90%;
margin: 0 5% 15px;
text-align: center;
font-size: 18px;
font-weight: 600;
}

.box_button {
z-index: 3;
position: relative;
display: block;
box-sizing: border-box;
width: 70%;
height: 36px;
margin: 0 auto 15px;
line-height: 36px;
color: #333;
background-color: #f4f4f4;
border-width: 1px;
border-style: solid;
border-radius: 18px;
text-align: center;
cursor: pointer;
}

.btn_1 {
border-color: #008CFF;
}

.btn_2 {
border-color: #F89A00;
}

.btn_active {
z-index: 4;
-webkit-animation: scale_up 0.6s linear;
animation: scale_up 0.6s linear;
}

@-webkit-keyframes scale_up {
0% {
-webkit-transform: scale(1,1);
transform: scale(1,1);
}
50% {
-webkit-box-shadow: 0px -8px 55px -15px rgba(0,0,0,0.75);
-moz-box-shadow: 0px -8px 55px -15px rgba(0,0,0,0.75);
box-shadow: 0px -8px 55px -15px rgba(0,0,0,0.75);
-webkit-transform: scale(1.1,1.1);
transform: scale(1.1,1.1);
}
100% {
-webkit-transform: scale(1,1);
transform: scale(1,1);
}
}

@keyframes scale_up {
0% {
-webkit-transform: scale(1,1);
transform: scale(1,1);
}
50% {
-webkit-box-shadow: 0px -8px 55px -15px rgba(0,0,0,0.75);
-moz-box-shadow: 0px -8px 55px -15px rgba(0,0,0,0.75);
box-shadow: 0px -8px 55px -15px rgba(0,0,0,0.75);
-webkit-transform: scale(1.1,1.1);
transform: scale(1.1,1.1);
}
100% {
-webkit-transform: scale(1,1);
transform: scale(1,1);
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="col_x4">
<div class="box">
<div class="box_top">
<img src="http://oregonaitc.org/wp-content/uploads/2016/02/potato.jpg" alt="product" />
</div>
<div class="box_content">
<div class="front">
<h2 class="box_title t1">Title</h2>
<p class="box_description">Description</p>
<div class="box_price">$10</div>
<a class="box_button btn_1">Add to cart</a>
</div>
<div class="back">
<h2 class="box_title t2">Flip side title</h2>
<p class="box_description">Product added to basket.</p>
<a class="box_button btn_1">Pay</a>
</div>
</div>
</div>
</div>

如何使用分配给框的 $(this) 选择器基于单击按钮来选择 div“返回”?我不想使用唯一 ID 或类来准确指定单击哪个框中的哪个按钮。

我使用 .closest 方法来选择 div“front”。我如何为 div“返回”做同样的事情?

最佳答案

您需要向上遍历到父元素,然后紧跟在兄弟元素之后定位

var content_back = $(this).parent().next('.back');

或者

var content_back = $(this). closest('.box_content').find('.back')

关于jquery - 如何在 jQuery 中基于 $(this) 选择器选择一个 div?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39799595/

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