我正在尝试将数组传递给我的.each JQuery代码,但它只传递数字0,1,2,3 ......而不是字符串值。
我定义变量的代码是:
var accessoriesCats = [ 'Beaded Accessories', 'Cufflinks', 'Flip Flops', 'Floral Accessories', 'Foot Jewelry', 'Hair Accessories', 'Hankies', 'Jewelry', 'Leg Garters', 'Purses', 'Shoe Stickers', 'Something Blue', 'Tiaras', 'Totes' ];
这是我的整个代码:
<script>
if (window.location.href.indexOf("category-s/2022.htm") != -1) {
var accessoriesCats = [ 'Beaded Accessories', 'Cufflinks', 'Flip Flops', 'Floral Accessories', 'Foot Jewelry', 'Hair Accessories', 'Hankies', 'Jewelry', 'Leg Garters', 'Purses', 'Shoe Stickers', 'Something Blue', 'Tiaras', 'Totes' ];
$('#content_area > table:nth-child(6) > tbody > tr > td > table:nth-child(1) > tbody > tr > td > table > tbody').find('a').each(function(accessoriesCats){
$(this).append('<span class="promo__text">' + accessoriesCats + '</span>');
$(this).removeClass('smalltext colors_text').addClass('subcatRollover');
});
}
</script>
基本上我想要的第一个.append是:
<span class="promo__text">Beaded Accessories</span>
但我得到了
<span class="promo__text">0</span>
答案 0 :(得分:1)
使用此:
<script>
if (window.location.href.indexOf("category-s/2022.htm") != -1) {
var accessoriesCats = [ 'Beaded Accessories', 'Cufflinks', 'Flip Flops', 'Floral Accessories', 'Foot Jewelry', 'Hair Accessories', 'Hankies', 'Jewelry', 'Leg Garters', 'Purses', 'Shoe Stickers', 'Something Blue', 'Tiaras', 'Totes' ];
$('#content_area > table:nth-child(6) > tbody > tr > td > table:nth-child(1) > tbody > tr > td > table > tbody').find('a').each(function(i){
$(this).append('<span class="promo__text">' + accessoriesCats[i] + '</span>');
$(this).removeClass('smalltext colors_text').addClass('subcatRollover');
});
}
</script>
答案 1 :(得分:0)
根据文档,数组中.each
的第一个参数是索引,第二个是项目(http://api.jquery.com/jquery.each/)
.each(function(index, accessoriesCats) {
^^^ADD INDEX