jQuery 子选择器帮助
一定有更好的方法来选择这个元素。有人可以帮我吗?我尝试过第 n 个孩子和最后一个孩子,但我没有得到它。
这是我的代码:
<li>
<div class="pic" style="width:300px; text-align:center;">
<img src='path/to/my/image.jpg' />
</div>
<p>
<span title="<?php echo $media_title; ?>">Title: <?php echo stringDisplay($media_title,12); ?></span><br />
<span title="<?php echo $media_filename; ?>">File: <?php echo stringDisplay($media_filename,18); ?></span><br />
Updated: <?php echo date("n/j/y", $lastUpdated).' at '.date("h:ia",$lastUpdated); ?>
</p>
<a class="link" href="javascript:;" onclick="alert($(this).parent().children('.pic').children().children().children().attr('src'));" style="margin-bottom:3px;">Preview</a>
</li>
我试图从下面的链接中选择第一个 div 中图像的图像源。我能想到的最好的办法是:
alert($(this).parent().children('.pic').children().children().children().attr('src'));
There must be a better way to select this element. Can someone please help me out. I've tried nth-child and last-child, but I'm not getting it.
Here's my code:
<li>
<div class="pic" style="width:300px; text-align:center;">
<img src='path/to/my/image.jpg' />
</div>
<p>
<span title="<?php echo $media_title; ?>">Title: <?php echo stringDisplay($media_title,12); ?></span><br />
<span title="<?php echo $media_filename; ?>">File: <?php echo stringDisplay($media_filename,18); ?></span><br />
Updated: <?php echo date("n/j/y", $lastUpdated).' at '.date("h:ia",$lastUpdated); ?>
</p>
<a class="link" href="javascript:;" onclick="alert($(this).parent().children('.pic').children().children().children().attr('src'));" style="margin-bottom:3px;">Preview</a>
</li>
I'm trying to select the image source of the image in the first div from the link below it. The best I could come up with is:
alert($(this).parent().children('.pic').children().children().children().attr('src'));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是您要找的吗?
注意,我强烈建议不要使用
onclick
来绑定您的Javascript事件处理程序。Is this what you're looking for?
N.B. I would strongly recommend not using
onclick
to bind your Javascript event handlers.这就是我要做的:
jQuery中的遍历方式有很多种:
This is how I would do it:
There are many ways to traverse in jQuery:
查找第一个具有图像元素的同级元素并返回 src 属性。
Find the first sibling with an image element and return the src attribute.