Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.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
Javascript 检索子imgsrc(jQuery)_Javascript_Jquery - Fatal编程技术网

Javascript 检索子imgsrc(jQuery)

Javascript 检索子imgsrc(jQuery),javascript,jquery,Javascript,Jquery,我有多个div,如下所示: <div><a href="our_work.html?clientid=39"> <img src="filestore/data_logos/39.gif" alt="client logo"/> </a></div> 警报总是返回为空,有什么想法吗?使用以下方法: $('#carousel div').click(function(event) { alert($(this).find('img'

我有多个div,如下所示:

<div><a href="our_work.html?clientid=39">
<img src="filestore/data_logos/39.gif" alt="client logo"/>
</a></div>
警报总是返回为空,有什么想法吗?

使用以下方法:

$('#carousel div').click(function(event) {
  alert($(this).find('img').attr('src'));
});
图像不是div的子对象。它们是div的子对象
,需要再向下一级。

尝试以下操作:

$('#carousel div').click(function(event) {
  alert($('img', this).attr('src'));
});
~Matt

,我的重点:

给定一个表示一组DOM元素的jQuery对象,.children()方法允许我们在DOM树中搜索这些元素的立即子元素

IMG是这样嵌套的:
DIV>A>IMG
;您需要的是
find()
而不是
children()

+1-
。children()
将只返回直接后代,其中
。find()
将搜索下面的所有元素;子女、孙辈等。
$('#carousel div').click(function(event) {
  alert($('img', this).attr('src'));
});