如何让$(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>
答案 0 :(得分:5)
将函数传递给html
方法并返回所需的文本:
$('.parent').find(".child1").html(function () {
return $(this).parent().find('.info').html();
});
来自official jQuery documentation:
.html( function )
:返回要设置的HTML内容的函数。接收集合中元素的索引位置和旧HTML值作为参数。 jQuery在调用函数之前清空元素;使用oldhtml参数引用以前的内容。在函数中,this
指的是集合中的当前元素。