我有多个div,如下所示:
<div><a href="our_work.html?clientid=39">
<img src="filestore/data_logos/39.gif" alt="client logo"/>
</a></div>如果你点击div层,我正在尝试获取图像src:
$('#carousel div').click(function(event) {
alert($(this).children('img').attr('src'));
});警报总是返回为空,有什么想法吗?
发布于 2010-02-03 01:16:29
使用以下命令:
$('#carousel div').click(function(event) {
alert($(this).find('img').attr('src'));
});这些图像不是div的子级...它们是<a>的子级,它是div的子级,需要再往下一层。
发布于 2010-02-03 01:16:45
试试这个:
$('#carousel div').click(function(event) {
alert($('img', this).attr('src'));
});发布于 2010-02-03 01:19:17
Straight from the horse's mouth,重点是我的:
给定一个表示一组DOM元素的jQuery对象,.children()方法允许我们在DOM树中搜索这些元素的immediate子项...
IMG是这样嵌套的:DIV > A > IMG;您需要的是find(),而不是children()。
https://stackoverflow.com/questions/2186096
复制相似问题