2339

我有一个类似于这样的布局:

<div id="..."><img src="..."></div>

并想使用 jQuery 选择器来选择点击img内的孩子div

要获得div,我有这个选择器:

$(this)

如何让孩子img使用选择器?

4

19 回答 19

2933

jQuery 构造函数接受调用的第二个参数context,该参数可用于覆盖选择的上下文。

jQuery("img", this);

这与这样使用相同.find()

jQuery(this).find("img");

如果您想要的 imgs只是单击元素的直接后代,您还可以使用.children()

jQuery(this).children("img");
于 2008-11-20T21:27:29.097 回答
482

你也可以使用

$(this).find('img');

这将返回所有imgs 的后代div

于 2008-11-20T21:23:15.393 回答
143

如果您需要获得img恰好下降一个级别的第一个,您可以这样做

$(this).children("img:first")
于 2011-07-21T18:47:31.383 回答
79

如果您的 DIV 标签紧跟在 IMG 标签之后,您还可以使用:

$(this).next();
于 2011-06-21T13:25:45.817 回答
74

直系子女是

$('> .child-class', this)
于 2012-07-16T20:07:51.357 回答
47

您可以找到img父 div 的所有元素,如下所示

$(this).find('img') or $(this).children('img')

如果你想要一个特定的img元素,你可以这样写

$(this).children('img:nth(n)')  
// where n is the child place in parent list start from 0 onwards

您的 div 仅包含一个img元素。所以下面这个是正确的

 $(this).find("img").attr("alt")
                  OR
  $(this).children("img").attr("alt")

但是,如果您的 div 包含更多img元素,如下所示

<div class="mydiv">
    <img src="test.png" alt="3">
    <img src="test.png" alt="4">
</div>

那么你不能使用上面的代码来查找第二个 img 元素的 alt 值。所以你可以试试这个:

 $(this).find("img:last-child").attr("alt")
                   OR
 $(this).children("img:last-child").attr("alt")

此示例显示了如何在父对象中找到实际对象的一般想法。您可以使用类来区分您孩子的对象。这既简单又有趣。IE

<div class="mydiv">
    <img class='first' src="test.png" alt="3">
    <img class='second' src="test.png" alt="4">
</div>

您可以按以下方式执行此操作:

 $(this).find(".first").attr("alt")

更具体为:

 $(this).find("img.first").attr("alt")

您可以使用 find 或 children 作为上述代码。有关更多信息,请访问儿童http://api.jquery.com/children/和查找http://api.jquery.com/find/。参见示例http://jsfiddle.net/lalitjs/Nx8a6/

于 2013-03-22T08:05:27.547 回答
35

在 jQuery 中引用子项的方法。我在下面的 jQuery 中总结了它:

$(this).find("img"); // any img tag child or grandchild etc...   
$(this).children("img"); //any img tag child that is direct descendant 
$(this).find("img:first") //any img tag first child or first grandchild etc...
$(this).children("img:first") //the first img tag  child that is direct descendant 
$(this).children("img:nth-child(1)") //the img is first direct descendant child
$(this).next(); //the img is first direct descendant child
于 2015-01-25T10:51:43.413 回答
31

试试这个代码:

$(this).children()[0]
于 2008-11-20T19:51:32.083 回答
31

在不知道 DIV 的 ID 的情况下,我认为您可以像这样选择 IMG:

$("#"+$(this).attr("id")+" img:first")
于 2008-11-20T19:56:20.250 回答
23

您可以使用以下任一方法:

1 查找():

$(this).find('img');

2 个孩子():

$(this).children('img');
于 2015-08-14T11:29:57.947 回答
21

jQueryeach是一种选择:

<div id="test">
    <img src="testing.png"/>
    <img src="testing1.png"/>
</div>

$('#test img').each(function(){
    console.log($(this).attr('src'));
});
于 2013-06-26T07:42:16.747 回答
15

您可以使用Child Selecor来引用父元素中可用的子元素。

$(' > img', this).attr("src");

以下是如果您没有参考$(this)并且您想在其他函数中参考img可用。div

 $('#divid > img').attr("src");
于 2014-07-25T21:56:40.380 回答
12

这也应该有效:

$("#id img")
于 2015-06-21T00:15:29.903 回答
8

这是一个功能代码,您可以运行它(这是一个简单的演示)。

当您单击 DIV 时,您会通过一些不同的方法获取图像,在这种情况下,“this”就是 DIV。

$(document).ready(function() {
  // When you click the DIV, you take it with "this"
  $('#my_div').click(function() {
    console.info('Initializing the tests..');
    console.log('Method #1: '+$(this).children('img'));
    console.log('Method #2: '+$(this).find('img'));
    // Here, i'm selecting the first ocorrence of <IMG>
    console.log('Method #3: '+$(this).find('img:eq(0)'));
  });
});
.the_div{
  background-color: yellow;
  width: 100%;
  height: 200px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="my_div" class="the_div">
  <img src="...">
</div>

希望能帮助到你!

于 2016-12-28T18:08:36.933 回答
5

<img><div>.

要查找元素,请使用.find().

为了保证您的代码安全,请使用.each().

在 0 个元素的情况下使用.find()and.each()一起防止空引用错误,<img>同时还允许处理多个<img>元素。

// Set the click handler on your div
$("body").off("click", "#mydiv").on("click", "#mydiv", function() {

  // Find the image using.find() and .each()
  $(this).find("img").each(function() {
  
        var img = this;  // "this" is, now, scoped to the image element
        
        // Do something with the image
        $(this).animate({
          width: ($(this).width() > 100 ? 100 : $(this).width() + 100) + "px"
        }, 500);
        
  });
  
});
#mydiv {
  text-align: center;
  vertical-align: middle;
  background-color: #000000;
  cursor: pointer;
  padding: 50px;
  
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

<div id="mydiv">
  <img src="" width="100" height="100"/>
</div>

于 2017-08-23T14:08:30.573 回答
4

$(document).ready(function() {
  // When you click the DIV, you take it with "this"
  $('#my_div').click(function() {
    console.info('Initializing the tests..');
    console.log('Method #1: '+$(this).children('img'));
    console.log('Method #2: '+$(this).find('img'));
    // Here, i'm selecting the first ocorrence of <IMG>
    console.log('Method #3: '+$(this).find('img:eq(0)'));
  });
});
.the_div{
  background-color: yellow;
  width: 100%;
  height: 200px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="my_div" class="the_div">
  <img src="...">
</div>

于 2017-03-05T13:27:33.393 回答
1

如果您的 img 恰好是 div 中的第一个元素,那么请尝试

$(this.firstChild);

$( "#box" ).click( function() {
  let img = $(this.firstChild);
  console.log({img});
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div id="box"><img src="https://picsum.photos/seed/picsum/300/150"></div>

于 2020-05-07T08:38:51.367 回答
0

使用本机 javascript,您可以使用

如果您有多个图像标签,请使用

this.querySelectorAll("img")

如果只有一个图像标签,那么我们

this.querySelector("img")

于 2021-03-26T07:10:29.973 回答
-1

你可以使用

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
 $(this).find('img');
</script>
于 2019-09-19T07:18:23.190 回答