Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/418.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 此对象引用在$each内不起作用_Javascript_Jquery - Fatal编程技术网

Javascript 此对象引用在$each内不起作用

Javascript 此对象引用在$each内不起作用,javascript,jquery,Javascript,Jquery,为什么foo不会被追加 $.ajax({ type: 'GET', dataType: 'json', cache: false, url: 'barfoochakalaka', success: function(response) { $.each

为什么foo不会被追加

$.ajax({
                type: 'GET',
                dataType: 'json',
                cache: false,
                url: 'barfoochakalaka',
                success:
                    function(response) {
                        $.each(response, function(index, val) {                             
                            $(this).parent().parent().append('foo');
                        });

                    }
})

响应可能不是数组,而是字符串。在执行.each()

var$this=$(“#选择器”).parent().parent()之前,请尝试将响应分配给元素,然后使用选择器获取该元素中的所有子元素;
$.ajax({
键入:“GET”,
数据类型:“json”,
cache:false,
url:“barfoochakalaka”,
成功:
功能(响应){
$。每个(响应、函数(索引、val){
var content=''+val.date+'、'+val.userid+'、'+val.status+'';
$this.append('foo');
});
}
})

编辑:


.parent().parent()
添加到原始选择器中,因此您不会为每个循环调用此函数,因为在
每个
中,
被设置为正在迭代的当前元素(),因此通常我们在进入
每个
循环之前将
定义为其他元素:

var that = this;
$.each(response, function(index, val) {
    var content = '<div class="link-history">'+ val.date + ', ' + val.userid + ', ' + val.status + '</div>';
    $(that).parent().parent().append('foo');
});

您的响应不是元素的对象,它很可能是一个字符串列表,可能带有选择器(?)如果是,则使用
$($(this))

您希望此
是什么?因为它前面的行出现错误。查看javascript控制台以查看错误消息是什么。在
$中。each()
引用当前由
$循环的元素。each
var that = this;
$.each(response, function(index, val) {
    var content = '<div class="link-history">'+ val.date + ', ' + val.userid + ', ' + val.status + '</div>';
    $(that).parent().parent().append('foo');
});
var that = this;
$.ajax({
    type: 'GET',
    dataType: 'json',
    cache: false,
    url: 'barfoochakalaka',
    success: function(response) {
        $.each(response, function(index, val) {
            var content = '<div class="link-history">' + val.date + ', ' + val.userid + ', ' + val.status + '</div>';
            $(that).parent().parent().append('foo');
        });

    }
})