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

时间:2016-09-30 20:27:36

标签: jquery html css

我的代码看起来像这样:

$(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“back”?我不想使用唯一的ID或类来准确指定单击哪个框的按钮。

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

4 个答案:

答案 0 :(得分:1)

您需要遍历父级,然后立即按照兄弟元素

进行定位
var content_back = $(this).parent().next('.back');

或者

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

答案 1 :(得分:0)

您可以在父容器上使用find()来查找后退元素 - 请参阅下面的示例:

&#13;
&#13;
$(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");
    var backContainer = $(this).parent().find('.back');
    console.log('backContainer',backContainer);
});
});
&#13;
<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>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

你可以这样写:

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

这将查找a节点的父节点,并找到具有back HTML类的节点。

答案 3 :(得分:0)

首先找到主容器并从那里开始工作:

$(".box_button").click(function(){
    var $box = $(this).parents('.box');
    var content_front = $box.find('.front');
    var content_back = $box.find('.back');

    content_front.addClass("flip_1");   
    content_back.addClass("flip_2");
});

$(document).ready(function(){
	"use strict";
	$(".box_button").click(function(){
        var $box = $(this).parents('.box');
		var content_front = $box.find('.front');
		var content_back = $box.find('.back');
		
		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>