简体   繁体   English

如何删除和附加元素li?

[英]How can I remove and append element li?

My html code like this : 我的html代码是这样的:

<ul class="list">
    <li id="thumb-view-1">view 1</li>
    <li id="thumb-view-2">view 2</li>
    <li id="thumb-upload-3">upload 3</li>
    <li id="thumb-view-4">view 4</li>
    <li id="thumb-view-5">view 5</li>
</ul>
<button id="test">Test</button>

My javascript code like this : 我的JavaScript代码是这样的:

<script type="text/javascript">
    $('#test').on("click", function(e){
        var a = 3;
        for(var i = 1; i <= 5; i++) {
            if(i == a) {
                $('#thumb-upload-'+i).remove();
                var res = '<li id="thumb-view-'+i+'">view '+i+'</li>';
                $('#thumb-view-'+(i-1)).after(res);
            }

        }
    });
</script>

Demo : https://jsfiddle.net/oscar11/eb114sak/ 演示: https : //jsfiddle.net/oscar11/eb114sak/

It works 有用

But my case is dynamic. 但是我的情况是动态的。 var a has value between 1 - 5. So var a can have value 1 or 2 or 3 or 4 or 5 var a的值介于1-5之间。因此var a的值可以为1或2或3或4或5

While ul tag has 5 li tag. 而ul标签有5个li标签。 And 5 li tag can have different id type 5 li标签可以具有不同的id类型

So in addition to the tag li above, I give an example of another form 因此,除了上面的标签li外,我还提供了另一种形式的示例

Like this : 像这样 :

<ul class="list">
    <li id="thumb-upload-1">upload 1</li>
    <li id="thumb-view-2">view 2</li>
    <li id="thumb-view-3">view 3</li>
    <li id="thumb-view-4">view 4</li>
    <li id="thumb-view-5">view 5</li>
</ul>

etc 等等

If like that, the result still wrong 如果那样,结果仍然是错误的

It seems it should call the li element based on a 现在看来,这应该叫基于li元素a

So if a = 3 then the third li tag is deleted and append 因此,如果a = 3,则删除第三个li标签并附加

But, I'm still confused 但是,我还是很困惑

How can I do it? 我该怎么做?

Instead of remove / append, try replaceWith: 而不是删除/追加,请尝试replaceWith:

$('#test').on("click", function(e){
    var a = 3;
    for(var i = 1; i <= 5; i++) {
        if(i == a) {
            var res = '<li id="thumb-view-'+i+'">view '+i+'</li>';
            $('#thumb-upload-'+i).replaceWith(res);             
        }           
    }
});

This will only replace matching #thumb-upload- elements, so it will handle your dynamic cases. 这只会替换匹配的#thumb-upload-元素,因此它将处理您的动态情况。

A simple solution could be to use replaceWith and index as 一个简单的解决方案是将replaceWithindex用作

var index = $( "ul.list" ).index( $("li[id^='thumb-upload']") );

This will get the index of li whose class starts with thumb-upload within your unordered list 这将获得li的索引,该li的类以您的无序列表中的thumb-upload开头

$("li[id^='thumb-upload']").replaceWith('<li id="thumb-view-'+index +'">view '+index +'</li>';)

And the above statement will replace that list item with your custom HTML 以上语句将用您的自定义HTML替换该列表项

Another simple solution is to just change the ID as I don't see other changes as 另一个简单的解决方案是只更改ID,因为我看不到其他更改

$("li[id^='thumb-upload']").attr('id', $("li[id^='thumb-upload']").attr('id').replace('upload','view'));

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

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