gpt4 book ai didi

javascript - MooTools inject json data into select 在IE中是倒退的

转载 作者:行者123 更新时间:2023-11-29 21:51:34 26 4
gpt4 key购买 nike

我从 PHP 返回一些 json 数据并使用 MooTools (1.5.1) 将 json 对象中的项目注入(inject)选择框。这很好用,但在 IE (11) 中,选择元素的填充与我的预期完全相反。 Firefox 和 Chrome 是正确的。我返回的数据是数据库键和日期/时间值的列表。每对将是 select 元素中的一行。这是返回的 json 数据的示例:

{"26165":"2015-03-03 06:11:17","26145":"2015-03-03 05:11:17","26125":"2015-03-03 04:11:17","26105":"2015-03-03 03:11:17"}

我无法发布图片...但是 Chrome 和 Firefox 会按照上面显示的确切顺序填充选择元素(最近的在顶部),而 IE 会以完全相反的顺序填充(最近的在底部)。用于将项目注入(inject)选择元素的代码如下。此代码片段位于“onSuccess” block 中的 MooTools“Request.JSON”中。

Object.each(list, function(value, key){
new Element('option', {'value':key, 'text':value}).inject(select_element);
});

再一次,这对 Chrome 和 Firefox 来说是完美的,所以我有理由相信我将值注入(inject) select 元素的方式是正确的,但为什么它在 IE 中会颠倒过来?我在想,也许 IE 在插入时按“值”对这些项目进行排序,这会产生颠倒的问题,因为上面显示的 json 数据是按 DESC 顺序排列的。 (Chrome/FF 的显示与插入的完全一样,也许 IE 会根据导致它们更改为 ASC 顺序的值进行排序?)。

感谢任何帮助或提示!

最佳答案

这与 MooTools 无关 - 但基本上,正如@sergio 所说,ECMA 脚本不保证在您遍历成员属性时索引的顺序。 FIFO 很好,但并非总是如此。

所有规范都说:

4.3.3 Object

An object is a member of the type Object. It is an unordered collection of properties each of which contains a primitivevalue, object, or function. A function stored in a property of anobject is called a method.

另请参阅第 12.6.4 节:

The mechanics and order of enumerating the properties ... is not specified.

http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-262.pdf

不同的浏览器处理方式不同,例如。当你 for (var key in obj) where key has numeric and string values 时,数字值将首先出现。 FireFox,它是先进先出。 IE - 其他东西。

唯一对您开放的是用 _ 填充键,以便将它们视为简单的字符串 - 或者使用像 [{key: 12313, value: 'some date '}]

关于javascript - MooTools inject json data into select 在IE中是倒退的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28825574/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com