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

标签 jquery css html

如何使 $(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();
});

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.

关于jQuery:如何使 $(this) 选择器工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27753907/

相关文章:

html - 通常在 li 内格式化文本(列表项)

c# - 如何使用 HTML Agility Pack 清理格式错误的 HTML

jquery - 加载时更改 div 中的 img src

html - ngx-bootstrap carousel - 如何修改指示器、Prev 和 Next 按钮

html - 如何根据各种设备调整 svg 的大小?

javascript - 如何在此 Accordion 中打开默认选项卡并在打开另一个选项卡时关闭该选项卡?

html - 带有背景图像 : 的可点击 <li>

javascript - 表单进度条

javascript - jquery.click 之后更新 HTML 页面

javascript - jquery中如何默认折叠所有 Accordion ?