JQuery - 函数不适用于 JSON 导入的数据

发布于 2024-11-04 12:52:07 字数 1501 浏览 0 评论 0原文

我有一个应用程序,它从 JSON 导入数据并生成(通过 JQuery 的 append())一些表示数据的 html 列表:

<ol id="iran">
    <li class="y1900"><p>Iran 1900 Revolution flag</p></li>
    <li class="y1906"><p>Iran 1906 Dictator flag</p></li>
    <li class="y1921"><p>Iran 1921 Revolution flag</p></li>
</ol>

我有一个函数 resizeFlags() (纯粹用于 UI)计算每个列表项的宽度并调整大小以适应一个屏幕。

resizeFlags() 函数在 $(document).ready() 期间调用(JSON 加载之后),并且可以很好地处理我的测试(静态)html 文件,但是当测试 html 被删除并使用 JSON 数据附加相同的 html 时不起作用 - 附加列表正确输入 DOM,但调整大小不会发生。

我有一个 resizeFlags() 的手动触发器(用于根据新的年份范围重新计算),并且可以按附加数据的预期工作。

我觉得这是我忽略的明显事情 - 我错过了什么?

编辑:

这是附加数据的代码(我已暂时将 resizeFlags() 移动到此函数中,通常在之后立即调用它:

function loadFlags() {
$.getJSON('data/flags.json',function(countriesdata){
    //for each flag
    $.each(countriesdata.countries,function(i,countries){
        $('#countries').append('<li id="'+countries.country+'">'+countries.country+'</li>')
        $('#flags').append('<ol id="'+countries.country+'"></ol>')
        $.each(countries.years,function(i,flagyears){
            $('#flags #'+countries.country).append('<li class="y'+flagyears.year+' '+flagyears.taxonomy+'"><p>'+flagyears.flagname+'</p></li>')
        });
    });
});
//resize from defaults
resizeFlags(totalYears);
}

I have an app that is importing data from JSON and generating (through JQuery's append())a number of html lists representing the data:

<ol id="iran">
    <li class="y1900"><p>Iran 1900 Revolution flag</p></li>
    <li class="y1906"><p>Iran 1906 Dictator flag</p></li>
    <li class="y1921"><p>Iran 1921 Revolution flag</p></li>
</ol>

I have a function resizeFlags() that (purely for UI) calculates the widths of each list item and resizes to fit everything into one screen.

The resizeFlags() function is called during $(document).ready() (after the JSON load) and works fine with my test (static) html file, but does not work when the test html is removed and the same html is appended using the JSON data - the appended lists enter the DOM properly, but the resizing does not happen.

I have a manual trigger for resizeFlags() (for recalculating based on a new year range) and that works as expected on the appended data.

I feel like it's something obvious that I've overlooked - what am I missing?

Edit:

Here's the code that appends the data (I've temporarily moved resizeFlags() into this function, normally it's called straight after:

function loadFlags() {
$.getJSON('data/flags.json',function(countriesdata){
    //for each flag
    $.each(countriesdata.countries,function(i,countries){
        $('#countries').append('<li id="'+countries.country+'">'+countries.country+'</li>')
        $('#flags').append('<ol id="'+countries.country+'"></ol>')
        $.each(countries.years,function(i,flagyears){
            $('#flags #'+countries.country).append('<li class="y'+flagyears.year+' '+flagyears.taxonomy+'"><p>'+flagyears.flagname+'</p></li>')
        });
    });
});
//resize from defaults
resizeFlags(totalYears);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

书间行客 2024-11-11 12:52:11

您需要在 getJSON 调用完成后(即插入函数结束时)调用 resizeFlags。否则,当调整大小时这些元素将不会出现

You need to call resizeFlags after the getJSON call completes, i.e at the end of the insertion function. Otherwise those element's won't be present when the resize occurs

一刻暧昧 2024-11-11 12:52:11

追加 JSON 数据后,您需要调用 resizeFlags() 函数。

如果resizeFlags()可以在CSS中完成,那么你就不需要再次调用resizeFlags()。

After you append the JSON data, you will need to call your resizeFlags() function.

If the resizeFlags() could be done in CSS, then you would not need to call resizeFlags() again.

南巷近海 2024-11-11 12:52:10

Ajax 调用(例如 $.getJSON)是异步的。
这意味着,即使您在触发文档就绪中的 resizeFlags() 函数之前进行 JSON 调用,响应也可能尚未到达。

确保在正确的时间调用 resizeFlags() 的唯一方法是将其放入成功处理程序中,如下所示:

$.getJSON('some-url', function(data) {
  //1. do something with the data (like build the list, append elements to the DOM)
  //...
  //2. call other UI stuff, after the elements created above are inserted in the DOM
  resizeFlags();
});

仅当 JSON 响应接收得非常快时(在更多代码被执行):

$.getJSON(...);
//some more code
resizeFlags();

Ajax calls (like $.getJSON) are asynchronous.
That means that even if you make the JSON call right before you trigger the resizeFlags() function in document ready the response might not have arrived yet.

The only way to make sure resizeFlags() is called at the right time is to put it in the success handler, like so:

$.getJSON('some-url', function(data) {
  //1. do something with the data (like build the list, append elements to the DOM)
  //...
  //2. call other UI stuff, after the elements created above are inserted in the DOM
  resizeFlags();
});

The following would work only if the JSON response is received really fast (before the more code stuff gets executed):

$.getJSON(...);
//some more code
resizeFlags();
无敌元气妹 2024-11-11 12:52:10

您正在 JSON 查询的回调函数之外调用 resizeFlags(totalYears);。即使在返回 JSON 数据之前也会立即调用此函数。每个循环完成后,将该行放入 JSON 成功回调中

You are calling resizeFlags(totalYears); outside of the callback function of the JSON query. This is called instantaneously even before the JSON data is returned. Put that line inside the JSON success callback once the each loop is compeleted

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文