Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在数组中的html元素上使用Jquery.addClass_Jquery_Arrays_Element - Fatal编程技术网

在数组中的html元素上使用Jquery.addClass

在数组中的html元素上使用Jquery.addClass,jquery,arrays,element,Jquery,Arrays,Element,我有一些填充数组的html图像标记。在页面上--显示数组中的三个元素。单击按钮后,我需要向它们添加一个类。如果从阵列中显示IMG,则每个IMG的类都不同。代码如下: $(document).ready(function(){ //populates array with images var shipImgs = $("#thumb_slider").children(); console.log(shipImgs); $.each(shipImgs,f

我有一些填充数组的html图像标记。在页面上--显示数组中的三个元素。单击按钮后,我需要向它们添加一个类。如果从阵列中显示IMG,则每个IMG的类都不同。代码如下:

$(document).ready(function(){        //populates array with images

    var shipImgs = $("#thumb_slider").children();
    console.log(shipImgs);

    $.each(shipImgs,function(i,elem){
        var tag = "<img src='" + $(elem).attr('src') + "' alt='space'/>"; // This is how I made the image tags.
        imgArr.push(tag);
    });
    console.log(imgArr);

});

$("#basic_ul").html(imgArr[b] + imgArr[a] + imgArr[c]); //displays the images
        imgArr[b].addClass("left_slot");
        imgArr[a].addClass("middle_slot");
        imgArr[c].addClass("right_slot");
$(document).ready(函数(){//用图像填充数组
var shipImgs=$(“拇指滑块”).children();
控制台日志(SHIPMGS);
$。每个(发货、功能(i、元素){
var tag=“”;//这就是我制作图像标记的方式。
imgArr.push(标签);
});
控制台日志(imgArr);
});
$(“#basic_ul”).html(imgar[b]+imgar[a]+imgar[c])//显示图像
imgArr[b].addClass(“左槽”);
imgArr[a].addClass(“中间位置”);
imgArr[c].addClass(“右槽”);
我还尝试了在数组项$(imgArr[b]).addClass(“left_slot”)周围使用选择器;但这也不起作用


任何建议都将不胜感激。我在stackoverflow上看过类似的问题,但运气不好。我已经研究这个项目有一段时间了,但什么也找不到。

您应该使用
each()
来迭代jQuery集合,而不是
$。each()

shipImgs.each(函数(){
var img=“”;
imgArr.push(img);
});

您的
imgArr
仅包含图像标记的HTML字符串数组

相反,如果将该字符串传递给jQuery函数,您将获得一个内存中的节点,然后可以将其添加到文档中

尝试将上面的代码更改为:

$(document).ready(function(){        //populates array with images
    var $basicUl = $('#basic_ul'); // cache the selector
    var shipImgs = $("#thumb_slider").children().each(function(index, element) {
        var newImg = $("<img />").attr({src: this.src, alt: 'space'}).addClass(index == 0 ? "middle_slot" : (index == 1 ? "left_slot" : "right_slot"));
        $basicUl.append(newImg);
    });
});
$(document).ready(函数(){//用图像填充数组
var$basicUl=$('#basic_ul');//缓存选择器
var shipImgs=$(“#拇指滑块”).children().each(函数(索引,元素){

var newImg=$(“您正在尝试将类添加到字符串中-
imgar[b]
是字符串而不是元素,您无法将类添加到字符串中。请尝试以下操作:

$(document).ready(function(){        //populates array with images

    var shipImgs = $("#thumb_slider").children();
    console.log(shipImgs);

    $.each(shipImgs,function(i,elem){
        var tag = $("<img src='" + $(elem).attr('src') + "' alt='space'/>"); // This is how I made the image tags.
        imgArr.push(tag);
    });
    console.log(imgArr);

});

$("#basic_ul").append(imgArr[b]);
$("#basic_ul").append(imgArr[a]);
$("#basic_ul").append(imgArr[c]);
imgArr[b].addClass("left_slot");
imgArr[a].addClass("middle_slot");
imgArr[c].addClass("right_slot");
$(document).ready(函数(){//用图像填充数组
var shipImgs=$(“拇指滑块”).children();
控制台日志(SHIPMGS);
$。每个(发货、功能(i、元素){
var tag=$(“”);//这就是我制作图像标记的方式。
imgArr.push(标签);
});
控制台日志(imgArr);
});
$(“#basic_ul”)。追加(imgArr[b]);
$(“#basic_ul”)。追加(imgArr[a]);
$(“#basic_ul”)。追加(imgArr[c]);
imgArr[b].addClass(“左槽”);
imgArr[a].addClass(“中间位置”);
imgArr[c].addClass(“右槽”);

对不起,如果您正在查看页面,您可能需要按“增加”按钮才能看到array@DigitalBrent没问题!我的代码是否有任何方面不清楚?您是否理解HTML字符串和实际DOM元素之间的重要区别,特别是与jQuery有关的区别?请告诉我是否可以澄清任何问题R
$(document).ready(function(){        //populates array with images

    var shipImgs = $("#thumb_slider").children();
    console.log(shipImgs);

    $.each(shipImgs,function(i,elem){
        var tag = $("<img src='" + $(elem).attr('src') + "' alt='space'/>"); // This is how I made the image tags.
        imgArr.push(tag);
    });
    console.log(imgArr);

});

$("#basic_ul").append(imgArr[b]);
$("#basic_ul").append(imgArr[a]);
$("#basic_ul").append(imgArr[c]);
imgArr[b].addClass("left_slot");
imgArr[a].addClass("middle_slot");
imgArr[c].addClass("right_slot");