简体   繁体   English

动态加载列表项并对它们进行排序,两个脚本可以分开工作,但不能一起工作。 (包括JSFiddle)

[英]Dynamically load list-items and sort them, 2 scripts both work apart, but they don't work together. (JSFiddle included)

I have made a script to load list-items dynamically via JSON and I found a script to sort them by date. 我制作了一个脚本,可以通过JSON动态加载列表项,并且找到了一个脚本,可以按日期对它们进行排序。

However, they don't work together, if I put them together the list-items get loaded but not sorted, 但是,它们不能一起使用,如果我将它们放在一起,则列表项会被加载但未排序,

Script 1 (to load the list-items): JSFiddle1 脚本1(加载列表项): JSFiddle1

Script 2 (to sort the list-items): JSFiddle2 脚本2(对列表项进行排序): JSFiddle2

As you can see, they both work. 如您所见,它们都起作用。 But when I put them together I just get: 但是,当我将它们放在一起时,我得到的是:

<ul class="datacursusul">
  <li class="21/05/2014">Wo 21 mei 2014</li>
  <li class="05/01/2014">Do 05 januari 2014</li>
</ul>

What I want: 我想要的是:

<ul class="datacursusul">
  <li class="05/01/2014">Do 05 januari 2014</li>
  <li class="21/05/2014">Wo 21 mei 2014</li>
</ul>

Could it be that the list hasn't loaded when the sort is being fired? 可能是在触发排序时未加载列表? Have you tried the sort call by adding a .done() call? 您是否通过添加.done()调用尝试了排序调用?

https://api.jquery.com/jQuery.get/ https://api.jquery.com/jQuery.get/

See the second to last example. 请参见倒数第二个示例。

I have updated your JSFiddle to combine the two scripts. 我已经更新了您的JSFiddle以结合这两个脚本。 I think the problem might have been that you were trying to sort the list before the content was loaded. 我认为问题可能出在您尝试在内容加载之前对列表进行排序。 The following line must be called after the list is populated with the loaded content: 用加载的内容填充列表 ,必须调用以下行:

$('.datacursusul').sortChildren(ddmmyyyClassSort);

Updated JSFiddle 更新了JSFiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM