如何通过使用jquery单击列表项,将一个列表项从一个无序列表“复制”并附加到另一个无序列表中?

如何通过使用jquery单击列表项,将一个列表项从一个无序列表“复制”并附加到另一个无序列表中?,jquery,append,playlist,audio-player,Jquery,Append,Playlist,Audio Player,我目前正在制作自己的自定义音频播放器。我不想使用jPlayer。我希望能够从ul中单击网页中的li,并将其附加到播放列表中。这是我的代码。这只是一些测试代码。如果有人能帮上忙,那就太棒了 下面是HTML: <h2>First List</h2> <p>Click a track below to add to 'Second List' below:</p> <div class="first"> <ul>

我目前正在制作自己的自定义音频播放器。我不想使用jPlayer。我希望能够从ul中单击网页中的li,并将其附加到播放列表中。这是我的代码。这只是一些测试代码。如果有人能帮上忙,那就太棒了

下面是HTML:

<h2>First List</h2>
<p>Click a track below to add to 'Second List' below:</p>
<div class="first">
    <ul>
        <li id="one" class="add"><a href"#">This is a paragraph.</a></li>
        <li id"two" class="add"><a href"#">This is another paragraph.</a></li>
    </ul>
</div>

<h2>Second List</h2>
<div class="second">
    <ul>
        <li>List item 1</li>
        <li>List item 2</li>
        <li>List item 3</li>
    </ul>
</div>
这是我的js:

function updateList() {
    var self = $(this);
    var holder = self.find("li");
    var trackId = holder.attr("id");
    var trackItem = $("#"+trackId).text();

    $('.second ul').append('<li>' + '<a href"#">' + trackItem + '</a>' + '</li>');
    $('.second ul li:last').hide().slideDown();
}

// Appending the new item
$('.add a').click(function() {
    updateList();
});

首先,对于您的特定问题,许多可能的解决方案中的一种可能如下所示:

$('.add').click(function() {
    $('.second ul').append($(this).removeClass('add').unbind('click'));  
});
您的代码存在一些主要问题。其中之一是,您正在updateList函数中引用它,该函数在这种情况下指向窗口,但与this不同,this指向您在$'中收到的单击LI。添加一个。单击。因此,如果您想在额外函数中处理添加,您必须将其值从单击函数传递给updateList

最后,因为这是第一个关于StAccess的问题,你可能会考虑使用合适的标签来解决你将来的问题,因为这个问题与音频、播放器或播放列表无关。这是一个简单的jquery问题

希望我能帮你一点忙,祝你在实现音频播放器方面好运