如何使 $(this) 选择器起作用?
j查询
$('.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
方法并返回您想要的文本:
$('.parent').find(".child1").html(function () {
return $(this).parent().find('.info').html();
});
来自 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.
关于jQuery:如何使 $(this) 选择器工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27753907/