【发布时间】:2015-10-26 11:56:32
【问题描述】:
我有这段代码,它显示了一个配置文件 div onclick 并更改了单击元素的文本。这种情况有多种。
<div class="show" data-target=".open1">View Profile</div>
<div class="info open1">Lorem ipsum</div>
当另一个 div 打开时,之前打开的 div 会关闭。然而,这方面的文字仍然是“密切的个人资料”。我想更改它,以便文本也更改回来。
知道我该怎么做吗?
var $bgs = $('.info');
var $show = $('.show');
$($show).click(function () {
var $target = $($(this).data('target')).stop(true).slideToggle();
$bgs.not($target).filter(':visible').stop(true, true).slideUp();
$(this).click(function(){
$(this).text(function(_, oldText) {
return oldText === 'Close Profile' ? 'View Profile' : 'Close Profile';
});
});
})
【问题讨论】:
-
你不需要在
$()中重新包装$show- 它已经是一个 jQuery 对象 -
不需要内部
.click(。只需检查文本并更新它。
标签: javascript jquery html css slidetoggle