简体   繁体   English

动态创建 <li> 在IE和Firefox及其他浏览器中的行为有所不同

[英]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中工作。

Additional HTML code as well as an example as per comment 其他HTML代码以及每个注释的示例

<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 this - posting an answer in case it helps somebody 我已解决此问题-发布答案以防万一

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.

相关问题 如果在使用document.write创建的页面上单击锚点,Firefox的行为与其他浏览器的行为不同。 有解决方法吗? - Firefox behaves differently from other browsers if anchors are clicked on a page which was created using document.write. Is there a workaround? IE中的PrtScn Button不像其他使用java脚本的浏览器那样 - PrtScn Button in the IE not behaving as other browsers using java script Firefox和其他浏览器的事件监听器是否有所不同? - Do Firefox and other browsers do event listeners differently? IE8解析这个简单的正则表达式与所有其他浏览器不同 - IE8 parses this simple regex differently from all other browsers Javascript动画:IE与其他浏览器 - Javascript animation: IE vs other browsers 动态创建的链接在 chrome 中工作但不在 firefox 或 IE 中工作 - Dynamically created link working in chrome but not in firefox or IE 角路由在不同的浏览器中表现得奇怪/不同 - Angular routing behaving strange/differently with different browsers 正则表达式在IE6 / IE7中的行为有所不同 - Regex behaving differently in IE6/IE7 Firefox与其他浏览器中的Javascript自动排序 - Javascript auto-sort in firefox vs Other browsers 在chrome和firefox中行为不同的简单javascript行 - Simple javascript line behaving differently in chrome and firefox
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM