Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.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
如何使用JQuery在特定div中获取img_Jquery_Jquery Selectors - Fatal编程技术网

如何使用JQuery在特定div中获取img

如何使用JQuery在特定div中获取img,jquery,jquery-selectors,Jquery,Jquery Selectors,我必须获取特定div中的所有图像标记ID。如何使用JQuery获取这些ID?粗略猜测,但请尝试: var imgIds = new Array(); $("div#divID img").each(function(){ imgIds.push($(this).attr('id')); }); 您没有给出div的名称,但我使用了divId作为div的id。只需更改它以满足您的需要。使用子选择器。所以你说我想要div#myDiv的所有儿童“img”元素 您可能应该使用数组,而不是字符串

我必须获取特定div中的所有图像标记ID。如何使用JQuery获取这些ID?

粗略猜测,但请尝试:

var imgIds = new Array();

$("div#divID img").each(function(){
    imgIds.push($(this).attr('id'));
});

您没有给出
div
的名称,但我使用了
divId
作为div的id。只需更改它以满足您的需要。

使用子选择器。所以你说我想要div#myDiv的所有儿童“img”元素


您可能应该使用数组,而不是字符串。啊,太酷了。通过.map()功能,我学到了一些新东西。但是,看看文档,为什么需要在最后调用.get()。我只是在没有它的情况下尝试了你的代码,效果很好。新手想要学习。当我替换$('img').doStuff()时,我想让它起作用;使用$(“#contents>img”).doStuff();它不再做任何事情了。我也尝试了单引号和div#contents。我的解决方案是使用后代选择器而不是child。这并不是选择div的所有子img元素,而是选择直接子元素。
$("#myDiv > img").css("border", "3px double red");
var arraysOfIds = $('#particularDivId img').map(function(){
                       return this.id;
                   }).get();

// arraysOfIds has now all the id's, access it as arraysOfIds[0], arraysOfIds[1]....  
function textOnly() {
            jQuery('#dvContent img').each(function () {
                jQuery(this).css('display', 'none');
            });
        }