未使用事件侦听器“this”中的 Javascript jquery 访问全局变量

我是初学者。我试图访问 jquery 事件列表器中的对象,但得到未定义或类型错误。我尝试了几种方法尝试以这种方式发送对象 {results: result} 但它不起作用,给了我未定义的错误。我找不到适合我的答案。提前致谢


jquery 整个代码


$(document).ready(function() {

//var $addhere = $("#addhere");

$.ajax({

    type: "GET",

    url: "/newsfeed/retrieve",

    //datatype: "JSON",

    success: function(datas){

        //console.log(data);

        // Check the length of success data.data

        const data = datas;

        var datalength = Object.keys(data.data).length;

        // Calling the result function

        var count = 0;

        result(data, datalength);

        reactions(data, datalength, count);


    }

});


function result(result, datalength){

    // Values are here im result

    console.log(result);

    var myPostHtml = '';

    // Values in the first place of object array

    //console.log(result.data[0].uploads);

    for(i=0;i<datalength;i++)

    {

      //if(result.data[i].uploads.attr("src", "No files uploaded")){

        //$("img").hide();

      //}

        myPostHtml += `<img src="${result.data[i].uploads}" id="contentAreaHomePageTweetPhoto" alt="post-image" class="img-responsive post-image" />

        <div class="post-container">

          <img src="${result.data[i].uploads}" id="" alt="user" class="profile-photo-md pull-left" />

          <div class="post-detail">

            <div class="user-info">

              <h5><a href="/timeline" id="" class="profile-link">${result.data[i].username}</a> <!--<span class="following">following</span>--></h5>

              <p class="text-muted"id="contentAreaHomePageTime">${result.data[i].createdAt}</p>

            </div>

     

月关宝盒
浏览 140回答 1
1回答

HUWWW

我会说你可能会被“关闭”绊倒。这里有一个外部循环,它发出异步 JavaScript 请求。神奇的是,他们可以使用当时的价值i。但是不能保证在data[]实际执行语句时数组实际上包含该元素。我在这里看到了各种其他印刷错误问题……result&nbsp;对比&nbsp;results等等。非常仔细地检查你的逻辑,并确保你完全理解它应该做什么。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript