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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3021152/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 14:44:11  来源:igfitidea点击:

How to get img inside a particular div using JQuery

jqueryjquery-selectors

提问by DEVOPS

I have to get all the image tags ids inside a particular div. How can I get that using JQuery?

我必须在特定 div 中获取所有图像标签 ID。我怎样才能使用 JQuery 得到它?

回答by Reigel

var arraysOfIds = $('#particularDivId img').map(function(){
                       return this.id;
                   }).get();

// arraysOfIds has now all the id's, access it as arraysOfIds[0], arraysOfIds[1]....  

回答by Jason Evans

Rough guess, but try:

粗略猜测,但请尝试:

var imgIds = new Array();

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

You haven't given the name of the div, but I've used divIdas the id of the div. Simply change that to suite your needs.

您没有给出 的名称div,但我已将其用作divIddiv 的 id。只需更改它即可满足您的需求。

回答by Markive

Use a child selector. So your saying I want all of the child 'img' elements of div #myDiv

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

$("#myDiv > img").css("border", "3px double red");

http://api.jquery.com/child-selector/

http://api.jquery.com/child-selector/

回答by Markive

function textOnly() {
            jQuery('#dvContent img').each(function () {
                jQuery(this).css('display', 'none');
            });
        }