Javascript 第N个孩子。创建一个div并填充它
我有一个函数,它将一个div(基本衬衫)附加到一个div(按钮)上Javascript 第N个孩子。创建一个div并填充它,javascript,jquery,Javascript,Jquery,我有一个函数,它将一个div(基本衬衫)附加到一个div(按钮)上 然后,我希望图像填充(基本衬衫)div。当我运行脚本时,图像被放置在“基本衬衫”div之后。您知道如何将它们放入“基本衬衫”而不是之后吗 这就是正在发生的事情: <div class="Button"> <div class="Base-Shirt"></div> <img class="2016" src="img/swatch/2016.jpg" title="Ruby_Red"
然后,我希望图像填充(基本衬衫)div。当我运行脚本时,图像被放置在“基本衬衫”div之后。您知道如何将它们放入“基本衬衫”而不是之后吗
这就是正在发生的事情:
<div class="Button">
<div class="Base-Shirt"></div>
<img class="2016" src="img/swatch/2016.jpg" title="Ruby_Red" alt="Main, Pique">
<img class="2017" src="img/swatch/2017.jpg" title="Khaki_Tan" alt="Main, Pique">
这就是我希望发生的事情:
<div class="Button">
<div class="Base-Shirt">
<img class="2016" src="img/swatch/2016.jpg" title="Ruby_Red" alt="Main, Pique">
<img class="2017" src="img/swatch/2017.jpg" title="Khaki_Tan" alt="Main, Pique">
</div>
作用
function parse(document){
//Product
$(document).find("Item").each(function(){
$(".Button").append('<div class="' + $(this).attr('name') +'"></div>');
});
//Swatch
$(document).find("Swatch").each(function(){
$(".Button:nth-child(1)").append(
'<img class="' + $(this).attr('name') + '" alt="' + $(this).attr('alt') + '" title="' + $(this).attr('title') + '" src="img/swatch/' + $(this).attr('name') + '.jpg">'
);
});
}//function parse End
函数解析(文档){
//产品
$(文档)。查找(“项”)。每个(函数(){
$(“.Button”).append(“”);
});
//斯沃琪
$(文档)。查找(“样例”)。每个(函数(){
$(“.Button:第n个子项(1)”).append(
''
);
});
}//函数解析结束
谢谢。您可以尝试
$(“.Button”)。儿童(“:first”)
将您放在div shirt元素上您可以尝试$(“.Button”)。儿童(“:first”)
将您放在div shirt元素上使用儿童
和eq
$(".Button").children(":eq(0)"); //<-- this will get you the first children(start form 0)
$(".Button").children(":eq(1)"); //<-- this will get you the second children
$(“.Button”).children(“:eq(0)”)// 使用children
和eq
$(".Button").children(":eq(0)"); //<-- this will get you the first children(start form 0)
$(".Button").children(":eq(1)"); //<-- this will get you the second children
$(“.Button”).children(“:eq(0)”)//谢谢,这是可行的,但是有没有办法用数字代替单词(1)not(:first)?你可以用$(“.Button”).children(:eq(0)”或$(“.Button:nth child(1)”)注意:.Button和:nth child选择器之间的空格。谢谢,这是可行的,但是有没有办法用数字代替单词(1)not(:first)?你可以用$(“.Button”).children(“:eq(0)”或$(“.Button:n个child(1)”)注意:.Button和:n个child选择器之间的空格。