简体   繁体   English

Internet Explorer前置要以不同的方式显示

[英]Internet Explorer prependTo displaying differently

I have the following HTML code:- 我有以下HTML代码:-

<ul id="list1">
    <li class="a">item 1</li>
    <li class="a">item 2</li>
    <li class="b">item 3</li>
    <li class="b">item 4</li>
</ul>

And the following script:- 和以下脚本:

<script type="text/javascript">
    $("document").ready(function() {
            $("#list1 li:last").prependTo("#list1 li:first");

    });
</script>

The intended display is:- 预期的显示是:-

预期的显示

But on IE I'm seeing:- 但是在IE上我看到:

IE浏览器

I'm using jQuery 1.10.2. 我正在使用jQuery 1.10.2。

Any clues? 有什么线索吗?

Looks like you are trying to move the last li to the first position, that means you need to prepend it to the ul element not to the first li 看起来您正在尝试将最后一个li移到第一个位置,这意味着您需要将其放在ul元素之前而不是第一个li之前

$("#list1 li:last").prependTo("#list1");

Demo: Fiddle 演示: 小提琴

which is the same as inserting it before the first element using insertBefore('#list1 li:first') 这与使用insertBefore('#list1 li:first')在第一个元素之前插入它相同

Why the display varies in browsers? 为什么显示在浏览器中会有所不同? Because you create an invalid html markup by appending an li to another li , li should be added to a ul or ol element 由于您是通过将li附加到另一个li来创建无效的html标记,因此应将li添加到ulol元素中

If you want to put the last li to the first position before the first li , try to use insertBefore() instead: 如果你想要把最后li到第一位置的第一前li ,尝试使用的insertBefore()代替:

$("document").ready(function() {
    $("#list1 li:last").insertBefore("#list1 li:first");
});

Demo 演示版

or you can use prependTo() which has the same effect: 或者您可以使用具有相同效果的prependTo()

$("document").ready(function() {
    $("#list1 li:last").prependTo("#list1");
});

Demo 演示版

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

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