[英]Trying to find an child image element passed using “this” in jquery
我正在尝试使用jQuery访问传递到事件处理程序中的图像。
因此,在这种情况下,“ this”将是div或li中包含img标记的div或li。
我已经尝试了以下访问器:
$(this).attr(),$(this)。('。imgClassName')。attr(),$(this:img).attr(),$(this.imgClassName).attr()
那些都不起作用。 这是我的脚本:
var closedText = 'Show All';
var openedText = 'Hide All';
var helpExpand = '/images/expand.png';
var helpCollapse = '/images/collapse.png';
$('li.helpList: a').click(function () {
$(this).next().slideToggle('fast', function () {
if (faqClosed) {
$(this).attr({
src: helpExpand,
title: openedText,
alt: openedText
});
} else {
$(this).attr({
src: helpCollapse,
title: closedText,
alt: closedText
});
}
});
return false;
});
这是行不通的。 但是,这确实可行(对于我的“隐藏/显示全部”):
$('#showAll').click(function () {
if (faqClosed) { // closed
$('li.helpList: div').show('fast'); faqClosed = false;
$('#showAll').text(openedText);
$(".helpToggle").attr({
src: helpExpand,
title: openedText,
alt: openedText
});
} else { // opened
$('li.helpList: div').hide('fast'); faqClosed = true;
$('#showAll').text(closedText);
$(".helpToggle").attr({
src: helpCollapse,
title: closedText,
alt: closedText
});
}
return false;
});
$('li.helpList:div')是打开/关闭的项目的容器。 其中是带有小箭头的img标签。 这是容器的HTML:
<li class="helpList">
<a href="#">
<img src="/images/collapse.png" class="helpToggle" alt="Contraer" />How do I do stuff?
</a>
<div class="helpContent">To do stuff, please do something.</div>
</li>
[UPDATE]
这是我的测试脚本,该脚本警告打开或关闭状态。 目前可以使用,但是仍然找不到图片(塌陷/expand.png文件不会隐藏,并且永远不会改变)
// Wire up hide/show to a.click event:
$('li.helpList: a').click(function () {
$(this).next().slideToggle('fast', function () {
if (faqClosed) {
alert('closed');
$(this).find('.helpToggle').hide();
$(this).find('img').attr({
src: helpExpand,
title: openedText,
alt: openedText
});
faqClosed = false;
} else {
alert('open');
$(this).find('.helpToggle').show();
$(this).find('img').attr({
src: helpCollapse,
title: closedText,
alt: closedText
});
faqClosed = true;
}
您可以使用:
$(this).find('img')...
获取所有作为其子级(任何深度)的图像。
您是否尝试过$(this).find('。imgClassName')。attr()?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.