当用户点击我的span
时带有文本“显示更多”的元素。我希望元素内的文本更改为“显示较少”。
$("div.fluid.examples span.fluid").click(function(e) {
this = $(this);
d = $(this).prevAll("div.fluid.examples p.hiddenp").length;
if (d>0) {
this.text("Show Less")
} else if (d<0) {
this.text("Show More")
}
$(this).prevAll(".hiddenp").fadeToggle(600);
});
最佳答案
为什么不按照常规方式进行操作,在同一次单击中切换文本和元素?
$("div.fluid.examples span.fluid").click(function() {
$(this).text(function(_,txt) {
return txt == 'Show Less' ? 'Show More' : 'Show Less';
});
$(this).prevAll(".hiddenp").fadeToggle(600);
});
关于javascript - 如何使用单击事件切换元素中的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16887225/