$(this).text()
允许您为元素指定纯文本内容。要附加图像,请使用$(this).append($(<img src='plus.gif'))
.
此外,不要使用$(this).text() == '-'
查看菜单是否已展开,而是将类附加到<a>
元素。
例如,我们可以用.addClass('expand')
表示菜单已展开,然后.hasClass('expand')
检查菜单是否已展开,最后'.removeClass('expand')
表示菜单不再展开)。
这是一个示例,使用虚拟图像进行说明:
JSFiddle
$(function(){
//starter plus image
var startPlus = $('<img>').attr('src','http://findicons.com/files/icons/1688/web_blog/48/add.png')
.css({'height': '20px', 'width': '20px'});
$('#accordion .fullChild>a.opener').addClass('box')
.append(startPlus);
$('#accordion .opener').click(function() {
//images for click event handler
var plusImg = $('<img>').attr('src','http://findicons.com/files/icons/1688/web_blog/48/add.png')
.css({'height': '20px', 'width': '20px'});
var minusImg = $('<img>').attr('src','http://findicons.com/files/icons/2083/go_green_web/64/subtract.png')
.css({'height': '10px', 'width': '10px'});
if($(this).hasClass('expand')) {
$(this).empty()
.append(plusImg)
.removeClass('expand');
} else {
$(this).empty()
.append(minusImg)
.addClass('expand');
}
});
});