[英]Dynamically created <li> behaving differently in IE vs Firefox &other browsers
I have created a sorting function to sort the li elements in a ul based on an id. 我创建了一个排序功能,用于基于id对ul中的li元素进行排序。
function noFilterForElements(){
/* Check where to append */
var listofelems = $('#ul-to-sort > li');
if (listofelems.length>0){
$('#ul-to-sort').html('');
$.each(listofelems,function(datakey,dataval){
/* Write out the elements without parents first */
console.log(dataval);
if ($(dataval).data('parent') == 0){
$('#ul-to-sort').append(dataval);
}
});
$.each(listofelems,function(datakey,dataval){
/* Write out the elements without parents first */
if ($(dataval).data('parent') != 0){
$('.parent-title[data-actid = "'+$(dataval).data('parent')+'"]').after(dataval);
}
});
}
}
The problem with this is that in FF & Chrome etc, the dataval automatically contains all the content / child nodes for the li element. 这个问题是在FF和Chrome等中,dataval自动包含li元素的所有内容/子节点。 In IE on the other hand, only the li element is taken. 另一方面,在IE中,仅使用li元素。 All children of the li element is not contained in the dataval variable. li元素的所有子元素均未包含在dataval变量中。
Tested on IE 8, 9, 10 and Edge (standards) 在IE 8、9、10和Edge(标准)上进行了测试
Any ideas on why this is happening and how I can overcome this problem please? 请问为什么会发生这种情况以及如何解决这个问题?
PS: I have tried iterating with listofelems.each() - same result; PS:我尝试过使用listofelems.each()进行迭代-相同的结果; working in FF & Chrome but not IE. 在FF和Chrome中工作,但不能在IE中工作。
<ul id="ul-to-sort">
<li class="parent-title" data-actid="world-1" data-parent = "0">Parent</li>
<li class="parent-title" data-actid="world-2" data-parent = "world-1">Child 1 of World 1</li>
<li class="parent-title" data-actid="world-3" data-parent = "world-1">Child 2 of world 1</li>
<li class="parent-title" data-actid="world-4" data-parent = "world-2">Child 1 of world 2</li>
<li class="parent-title" data-actid="world-5" data-parent = "world-2">Child 2 of world 2</li>
<li class="parent-title" data-actid="world-6" data-parent = "world-1">Child 3 of world 1</li>
<li class="parent-title" data-actid="world-7" data-parent = "world-1">Child 4 of world 1</li>
<li class="parent-title" data-actid="world-8" data-parent = "world-4">Child 1 of world 4</li>
</ul>
Tested with JsFiddle and it is re-arranging the child elements. 经过JsFiddle的测试,它正在重新排列子元素。
I have resolved the problem by NOT setting the ul element to empty. 我没有通过将ul元素设置为空来解决该问题。 Ie $('#ul-to-sort').html('');
即$('#ul-to-sort').html('');
Once i removed this, and taking the advice of @muuvmuuv & @Moshin, I re-wrote the function like so: 一旦删除了此代码,并听取@muuvmuuv&@Moshin的建议,我就重新编写了如下函数:
function noFilterForElements(){
/* Check where to append */
var listofelems = $('#ul-to-sort > li');
$(listofelems).each(function(elemkey,elemval){
if ($(elemval).data('parent') == 0){
$('#ul-to-sort').append(elemval);
} else {
$('.parent-title[data-actid = "'+$(elemval).data('parent')+'"]').after(elemval);
}
});
}
The ul is not building upon itself - so even on refresh it creates new elements - which is why i initially set the element .html('')
to empty. ul并不是建立在自身之上的-因此即使刷新,它也会创建新的元素-这就是为什么我最初将元素.html('')
为空的原因。 In theory when I append a new element it should create a duplicate - but it is not - which is good. 从理论上讲,当我添加一个新元素时,它应该创建一个副本-但不是-很好。 I suspect that becasue the element details are the same (no change to the original element - just re-ordering it), it is not creating a new element with the same parameters. 我怀疑由于元素详细信息相同(对原始元素没有更改-只是对其进行了重新排序),因此它没有创建具有相同参数的新元素。 (This is a guess - if anybody can help us understand why this is the case, please feel free to edit this answer out). (这是一个猜测-如果有人可以帮助我们了解为什么会出现这种情况,请随时修改此答案)。
Thanks for all your help. 感谢你的帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.