Home > Backend Development > PHP Tutorial > javascript-怎么动态增加元素顺序?

javascript-怎么动态增加元素顺序?

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-02 11:30:10
Original
1141 people have browsed it

javascriptphp前端jquery

<code>     <meta http-equiv="Content-Type" content="text/html; charset=utf-8">    <title>JQuery Test</title>    <script src="../behappy/jquery-2.1.4.min.js" charset="utf-8"></script>    <script>        $(function() {            n = 1;                $(".click").click(function()                {                    $(".aa").append('<li class="bb" id="选项'+n+'">'+n+'------<span class="del">删除');                    n++;                });        });        $(document).on('click', '.del', function() {            var num = $(this).parent("li").html();            num = parseInt(num);            $(".aa li").each(function(key,item){                if(key+1 > num){                    $(item).html(num+'------<span class="del">删除');                    $(item).attr('id',"选项"+num);                    num++;                }            });            $(this).parent("li").remove();        });    </script><div class="click">Click</div>
<ul class="aa"></ul></code>
Copy after login

点击click增加li,点击删除能够更新li里面的数字与属性ID,但是增加后还是按照原来的顺序增加,请问该怎么改?
例:
1------删除
2------删除
3------删除
4------删除
5------删除
6------删除
7------删除
8------删除
9------删除
10------删除

点击删除能动态改变数字:
1------删除
2------删除
3------删除
4------删除
5------删除
6------删除
7------删除

再点击增加:

1------删除
2------删除
3------删除
4------删除
5------删除
6------删除
7------删除
11------删除
12------删除
13------删除

增加因为是n++判定,所以并没有动态改变

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template