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

时间:2015-01-03 10:36:23

标签: jquery css html5

如何让$(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>

1 个答案:

答案 0 :(得分:5)

将函数传递给html方法并返回所需的文本:

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

JSFiddle Demo


来自official jQuery documentation

  

.html( function ) :返回要设置的HTML内容的函数。接收集合中元素的索引位置和旧HTML值作为参数。 jQuery在调用函数之前清空元素;使用oldhtml参数引用以前的内容。在函数中, this 指的是集合中的当前元素。

相关问题