【发布时间】:2012-12-30 13:31:01
【问题描述】:
此功能显示 arrow-img-down(“显示”图标)。当我点击它打开滑动 div 然后 img 变成 arrow-img-up (一个“隐藏”图标)
我想在此 img 中添加替换 .src 功能,并将鼠标悬停在当前 img 上,如下所示:
$(".showhide").live('click', function () {
if ($(this).attr("class") == "showhide") {
this.src = this.src.replace("img/show_btn.png", "img/hide_btn.png");}
else {
this.src = this.src.replace("img/hide_btn.png", "img/show_btn.png");}
$(this).toggleClass("img/show_btn.png");
});
如何添加?
【问题讨论】:
-
最好使用
hasClass而不是if ($(this).attr("class") == "showhide") { -
请注意,应该使用
hasClass的主要原因是因为如果向元素添加第二个类,$(this).attr("class") == …将不起作用。 -
jquery 网站说 live 已被弃用,应该改用 on。 api.jquery.com/on
标签: jquery