使用嵌套的 $(this) 语句时获取 .text() 值



单击<a>标签时,我想获取该链接的文本值并将其放置在父链接内的另一个<div>(.current-selection(中。但是,我还需要fadeOut()父级,以允许此更改在视线之外发生,然后再将其与fadeIn()一起恢复。我可以让它单独工作,但在将文本更改放在 fadeOut(( 函数中时则不行。

下面的代码获取父项的文本,而不是最初单击的标签。像这样嵌套时,我可以做些什么不同的事情?

谢谢。

$('.filter').on( 'click', 'a', function() {
// Fadeout text to allow for hidden text change
$(this).parent.fadeOut(function() {
// Change text of the current selection to match the item just clicked (not working)
$('.filter').find('.current-selection').text($(this).text());   
}).fadeIn();
});

将文本放在一个额外的变量中

$('.filter').on( 'click', 'a', function() {
var value = $(this).html();
$(this).parent().fadeOut(function() {
$('.filter .current-selection').text(value);   
}).fadeIn();
});

最新更新