[英]JQuery: How to use a for loop variable in a jquery selector?
這是我的第一篇文章。
我正在嘗試制作一個照相館,但下面的代碼可用於我的網站。 我希望當在.preview類中懸停相同的nth-child時,同一nth-child在.gallery中受到影響。
$(document).ready(function() {
$(".preview > :nth-child(1)").mouseover(function() {
$(".gallery > :nth-child(1)").css("opacity","1");
$(".gallery :not( > :nth-child(1))").css("opacity","0");
});
$(".preview > :nth-child(2)").mouseover(function() {
$(".gallery > :nth-child(2)").css("opacity","1");
$(".gallery :not( > :nth-child(2))").css("opacity","0");
});
$(".preview > :nth-child(3)").mouseover(function() {
$(".gallery > :nth-child(3)").css("opacity","1");
$(".gallery :not( > :nth-child(3))").css("opacity","0");
});
$(".preview > :nth-child(4)").mouseover(function() {
$(".gallery > :nth-child(4)").css("opacity","1");
$(".gallery :not( > :nth-child(4))").css("opacity","0");
});
});
然后,我想到了一種使用for循環的更簡單方法。 稍后我計划將更多孩子添加到.gallery和.preview中,因此for循環將使代碼更簡單。 我認為在下面的代碼中問題出在for循環變量i中。 您能否看下面的代碼,看看我在做什么錯?
$(document).ready(function() {
for (i = 1; i < preview.length; i++) {
var selector1 = ".preview > :nth-child(" + i + ")";
var selector2 = ".gallery > :nth-child(" + i + ")";
var selector3 = ".gallery :not( > :nth-child((" + i + "))";
$(selector1).mouseover(function() {
$(selector2).css("opacity","1");
$(selector3).css("opacity","0");
});
}
});
編輯:這是適用於的HTML:
<div class="gallery">
<div></div>
<div></div>
<div></div>
<div></div>
<div class="preview">
<div></div>
<div></div>
<div></div>
<div></div>
</div>
</div>
編輯:這是CSS
.gallery > :nth-child(1) {
background-image: url('https://sp.yimg.com/ib/th?id=HN.607997559404560656&pid=15.1&P=0');
height: 500px;
background-size: cover;
opacity: 1;
}
.gallery > :nth-child(2) {
background-image: url('http://www.kamionek.waw.pl/images/stories/2012/stadion_narodowy_ii_2012_kamionek_0001.jpg');
height: 500px;
background-size: cover;
position: relative;
top: -500px;
opacity: 0;
margin-bottom: -500px;
}
.gallery > :nth-child(3) {
background-image: url('http://www.twojezaglebie.pl/wp-content/uploads/2012/02/stadion-narodowy-luty-2012_6521.jpg');
height: 500px;
background-size: cover;
position: relative;
top: -500px;
opacity: 0;
margin-bottom: -500px;
}
.gallery > :nth-child(4) {
background-image: url('http://upload.wikimedia.org/wikipedia/commons/a/ac/Stadion_Narodowy_w_Warszawie_20120422.jpg');
height: 500px;
background-size: cover;
position: relative;
top: -500px;
opacity: 0;
margin-bottom: -500px;
}
.preview {
height: 100px;
width: 100px;
width: 100%;
}
.preview div {
height: 100px;
width: 100px;
background-size: cover;
display: inline;
float: left;
}
.preview > :nth-child(1) {
background-image: url('https://sp.yimg.com/ib/th?id=HN.607997559404560656&pid=15.1&P=0');
}
.preview > :nth-child(2) {
background-image: url('http://www.kamionek.waw.pl/images/stories/2012/stadion_narodowy_ii_2012_kamionek_0001.jpg');
}
.preview > :nth-child(3) {
background-image: url('http://www.twojezaglebie.pl/wp-content/uploads/2012/02/stadion-narodowy-luty-2012_6521.jpg');
}
.preview > :nth-child(4) {
background-image: url('http://upload.wikimedia.org/wikipedia/commons/a/ac/Stadion_Narodowy_w_Warszawie_20120422.jpg');
}
您可以使用現有的系統來執行此操作,例如: http : //jsfiddle.net/TrueBlueAussie/wn3c84ke/5/
$(document).ready(function () {
$(".preview div").mouseover(function () {
var $previews = $('.preview div');
var $children = $(".gallery").children().not('.preview');
var $selected = $children.eq($previews.index(this));
$children.not($selected).css("opacity", "0");
$selected.css("opacity", "1");
});
});
它使用懸停的索引位置作為選擇索引,但實際上我會推薦這樣的數據驅動方法: http : //jsfiddle.net/TrueBlueAussie/wn3c84ke/7/
HTML:
<div class="gallery">
<div id="one"></div>
<div id="two"></div>
<div id="three"></div>
<div id="four"></div>
<div class="preview">
<div data-target="#one"></div>
<div data-target="#two"></div>
<div data-target="#three"></div>
<div data-target="#four"></div>
</div>
</div>
jQuery的
$(document).ready(function () {
$(".preview > div").mouseover(function () {
var $children = $(".gallery > *:not(.preview)");
var $selected = $($(this).attr("data-target"));
$children.not($selected).css("opacity", "0");
$selected.css("opacity", "1");
});
});
一種使用循環的方法是:
$(document).ready(function() {
$(".preview div").each(function(index,object){
$(object).mouseover(function() {
$(".gallery div").css("opacity","0");
$(".gallery div")[index].css("opacity","1");
});
});
});
嘗試這個:
$(".preview").on("mouseover", function () {
$(".gallery").children().index(this).css("opacity", "0");
$(this).children().css("opacity", "1");
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.