jQuery:如何使 $(this) 选择器工作?

jQuery: How to make $(this) selector work?

如何使 $(this) 选择器起作用?

jQuery

$('.parent').find(".child1").html($(this).parent().find('.info').html());

例如 HTML

 <div class= "parent>
 <div class="child1"></div>
 <div class="child2"></div>
 <div class="child2"></div>
 <div class= "info"></div>
 </div>

将函数传递给您的 html 方法和 return 您想要的文本:

$('.parent').find(".child1").html(function () {
    return $(this).parent().find('.info').html();
});

JSFiddle Demo.


来自official jQuery documentation

.html( function ): A function returning the HTML content to set. Receives the index position of the element in the set and the old HTML value as arguments. jQuery empties the element before calling the function; use the oldhtml argument to reference the previous content. Within the function, this refers to the current element in the set.