我如何剪接我的阵列? [英] How do I splice my array?

查看:122
本文介绍了我如何剪接我的阵列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个收藏夹功能,但希望用户能够删除它们。

I have a favourites feature, but want the user to be able to remove them.

这是什么样子:

所以,我想实现是它调用remove函数的每个项目下的删除链接,所以删除该实体。

So what I want to achieve is a "Remove" link under each item which calls the remove function, and so removes that entity.

下面是我的JS:

function updateFavourite(video) {
    document.getElementById('favourite').onclick = function () {
        if ($.grep(myfavourite, function (item) {
            return item["id"] == video["id"];
        }).length == 0) {
            blacklist[video["id"]] = true;
            myfavourite.push(video);
            var html = "<li class=\"history\">" +
                "<img class= \"img-rounded\" src=\"{0}\"/>" +
                "<p><b title=\"{2}\"><a class=\"extendedLink\" href=\"javascript:watchFavouriteVideo(\'{1}\');\"><span></span>{2}</a></b><br>" +
                "by {3}<br>" +
                "{4} | {5} views</p>" +
                "</li>";

            $("#myfavourite").prepend(html.format(video["thumbnail"],
                video["id"],
                video["title"],
                video["uploader"],
                video["length"],
                video["views"]));
        }
    }
}

function remove(video) {
    document.getElementById('remove').onclick = function () {
        myfavourite.splice(video, 1);
    }
}

问题是,它不会删除该视频,不知道如何添加删除的文本为每个实体。

The problem is that it does not remove the video, and don't know how to add the "Remove" text for each entity.

推荐答案

下面是一个例子。

HTML

<div id="favourites"></div>
<div id="displayList"></div>

CSS

#favourites {
    width:auto;
    height:100px;
}
.favourite {
    width:auto;
    height: auto;
    margin-right:10px;
    background-color:cyan;
    float:left;
}
.title {
    width:auto;
    height: auto;
    background-color:red;
    border:0px;
    text-align:center;
}
.picture {
    width:50px;
    height: 50px;
    background-position:center;
    display:block;
    margin:0 auto;
}
.remove {
    width:auto;
    height: auto;
    text-align:center;
}
.remove:hover {
    cursor:pointer;
    background-color:yellow;
}
#displayList {
    min-height:20px;
    clear:both;
    border:1px solid black;
}

的JavaScript

Javascript

var picsArray = [
        'http://upload.wikimedia.org/wikipedia/commons/1/1b/Beys_Afroyim_with_son_%28cropped%29.jpg',
        'http://upload.wikimedia.org/wikipedia/commons/8/8a/Tammam_Salam.jpg',
        'http://upload.wikimedia.org/wikipedia/commons/2/27/Ratusz2007.jpg',
        'http://upload.wikimedia.org/wikipedia/commons/6/60/GPN-2000-001979.jpg'
    ],
    list = picsArray.slice(),
    favourites = document.getElementById('favourites'),
    displayList = document.getElementById('displayList');

function emptyNode(node) {
    while (node.firstChild) {
        node.removeChild(node.firstChild);
    }
}

function updateDisplayList() {
    emptyNode(displayList);
    list.map(function (entry) {
        return entry.split('/').slice(-1)[0];
    }).forEach(function (shortEntry) {
        var p = document.createElement('p');

        p.appendChild(document.createTextNode(shortEntry));
        displayList.appendChild(p);
    });
}

list.forEach(function (pic) {
    var favourite = document.createElement('div'),
        title = document.createElement('div'),
        img = document.createElement('img'),
        remove = document.createElement('div');

    favourite.className = 'favourite';
    title.className = 'title';
    img.className = 'picture';
    remove.className = 'remove';

    title.appendChild(document.createTextNode('Favourite'));
    favourite.appendChild(title);

    img.src = pic;
    favourite.appendChild(img);

    remove.appendChild(document.createTextNode('Remove'));
    remove.addEventListener('click', function (e) {
        e.target.parentNode.parentNode.removeChild(e.target.parentNode);
        list = list.filter(function (ele) {
            return ele !== e.target.previousSibling.src;
        });

        updateDisplayList();
    }, false);

    favourite.appendChild(remove);
    favourites.appendChild(favourite);
});

updateDisplayList();

的jsfiddle

这篇关于我如何剪接我的阵列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆