【发布时间】:2011-08-22 18:05:00
【问题描述】:
我正在尝试访问在 jQuery 中使用 this 传递给事件处理程序的图像。
因此,在这种情况下,“this”是一个 div 或 li,在 div 或 li 中包含一个 img 标签。
我尝试了以下访问器:
$(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>
[更新]
这是我的测试脚本,它根据状态提醒打开或关闭。这目前有效,但仍然找不到图像(collapse/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;
}
【问题讨论】:
标签: jquery