问问题
70918 次
8 回答
26
It's because the image tag is inside the ul tag. next only looks for tags at the same level. You'll have to use
var alt = $(this).children("img").attr("alt");
EDIT: included the additional " that was missing
于 2010-10-07T16:50:20.207 回答
8
$(this).find('img').attr('alt');
于 2012-05-04T10:56:22.593 回答
3
于 2010-10-07T17:00:33.527 回答
3
Try $(this).find('img')
instead to select some element within the link.
于 2010-10-07T16:50:29.960 回答
2
没关系。我刚刚明白了...我意识到这img
不是兄弟姐妹,<a>
而是孩子<a>
这是正确的语法
var alt = $(this).children("img").attr("alt");
于 2010-10-07T16:51:52.843 回答
1
试试这个测试和它的工作!
<div id="album-artwork">
<ul>
<li><a href="javascript:void(0);"><img src="images/ajax-loader.gif" alt="ajax-loader" /></a></li>
<li><a href="javascript:void(0);"><img src="images/close.png" alt="close" /></a></li>
<li><a href="javascript:void(0);"><img src="images/copy.png" alt="copy" /></a></li>
</ul>
</div>
和 jQuery 代码是
$(document).ready(function() {
$("#album-artwork a").click(function(e) {
e.preventDefault();
var src = $(this).attr("href");
//var alt = $(this).next("img").attr("alt");
var alt = $(this).children().attr("alt");
//find function also do the same thing if you want to use.
/*var alt = $(this).find("img").attr("alt");*/
alert(src); // ok!
console.log(src); // ok!
alert(alt); //output: not undefined now its ok!
console.log(alt); //output: not undefined now its ok!
});
});
该children()
函数找到任何孩子,但如果你想找到任何特定的孩子,请定义孩子的名字,像这样children("img")
OR children("button")
。
于 2014-02-07T05:13:54.050 回答
1
.next 不会选择下一个元素,即下一个 Li 而不是 IMG?
于 2010-10-07T16:51:29.437 回答
0
Dont complicate:
$('img').on({'click': function(){
var alt = this.alt;
于 2019-05-06T21:51:20.247 回答