如何使用jQuery在div中获取数据

Jam*_*den 2 html each jquery dom parent-child

我有以下内容:

jQuery('.activity-feed .comments div > li').each(function() {
 // Refer to each piece of data here
});
Run Code Online (Sandbox Code Playgroud)

我遇到的问题是,对于这些div中的每一个,我想从孩子的孩子等中提取一些数据等.我如何引用这些DOM元素?我试过了:

jQuery(this.find(".comment-container .note .activity-event div .meta a").text());
Run Code Online (Sandbox Code Playgroud)

但这没效果.我只需要将数据删除到变量中.我确信这很简单,但我之前没有做过任何类似的事情,我搜索的内容并没有完全覆盖它.


请注意,我知道$()会比jQuery()好,但它不适用于我操作的系统.

Ror*_*san 6

thiseach迭代中将引用DOM元素.如果要在该元素上使用jQuery方法,则需要将其包装在jQuery对象中$(this):

jQuery(this).find(".comment-container .note .activity-event div .meta a").text();
Run Code Online (Sandbox Code Playgroud)

请注意,我知道$()会比jQuery()好,但它不适用于我操作的系统.

默认情况下,jQuery对象会传递给DOMReady处理程序,因此您仍然可以$仅在该处理程序的范围内使用该变量.这不应该干扰您拥有的任何其他脚本.试试这个:

jQuery(function($) {
    $('.activity-feed .comments div > li').each(function() { 
        var foo = $(this).find(".comment-container .note .activity-event div .meta a").text();
    });
});
Run Code Online (Sandbox Code Playgroud)