jQuery 从不同的 div 单击后附加到 div

jQuery append to div on click from the different div

我有 div 个动态插入的在线用户:

<div id="users">
  <div class="privateMessage" data="John">John</div>
  <div class="privateMessage" data="Maria">Maria</div>
  <div class="privateMessage" data="Tony">Tony</div>
</div>

那我有一个div私信:

<div id="messageBox">
</div>

现在,我正在努力如何在单击用户时在 messageBox 中动态附加 div

我需要的是下面这个:

<div id="messageBox">
   //when I click on John from users div this below should be appended
   <div class="private-chat" data-conversation-between="John"></div>
  //when I click on Maria from users div this below should be appended and John above
  //will be hidden
   <div class="private-chat" data-conversation-between="Maria"></div>
  //when I click on Tony from users div this below should be appended and John and Maria
  //will be hidden
   <div class="private-chat" data-conversation-between="Tony"></div>
</div>

无论我怎样尝试,messageBox 中的 div 都被追加了不止一次。

有人可以用 jQuery 帮助我解决这个问题吗?

Link: fiddle

这样的事情怎么样?

http://jsfiddle.net/thetimbanks/hfuurcL7/

click 事件被委托,因为用户可以动态添加到列表中。我还在 messageBox 中搜索该用户的现有 div,以便不添加另一个用户。

在这里添加代码,不仅仅是 link 到 fiddle:

HTML

<div id="users">
  <div class="privateMessage" data-user="John">John</div>
  <div class="privateMessage" data-user="Maria">Maria</div>
  <div class="privateMessage" data-user="Tony">Tony</div>
</div>

<div id="messageBox">
</div>

js

$("#users").on("click", ".privateMessage", function() {

    var user = $(this),
        private_chat = $("#messageBox .private-chat[data-conversation-between='" + user.data("user") + "']");

    if (private_chat.length == 0) {
        private_chat = $('<div class="private-chat" data-conversation-between="' + user.data("user") + '">Chat with ' + user.data("user") + '</div>');
        $("#messageBox").append(private_chat);
    }

    private_chat.show().siblings().hide();
});

在评论中进行简短说明后,我发布了一个可行的解决方案:

$('.privateMessage').on('click', function (e) {
  $messageBox = $('#messageBox');
  var whoIsIt = $(this).attr('data');

  var isAlreadyThere = $messageBox.find('div[data-conversation-between="' + whoIsIt + '"]').length;

  if (isAlreadyThere == 0) {
    $messageBox.append('<div class="private-chat" data-conversation-between="' + whoIsIt + '"></div>');
  }
});

jsfiddle: http://jsfiddle.net/pLe01k57/2/

基本上:检查 #messageBox 是否已经与点击的用户进行对话 (div),如果没有 - 将其附加到那里。

这个怎么样?

$('.privateMessage').on('click', function (e) {
    var whoIsIt = $(this).attr('data');
    $('#messageBox').append('<div class="private-chat" data-conversation-between="' + whoIsIt + '"></div>');
    $(this).unbind();
});

https://jsfiddle.net/lemoncurry/5cq2sw8m/1/

基本上是 bardzusny 上面的解决方案加上一个 $(this).unbind().

您应该避免以这种方式使用 data 属性。

Read more about .data() attribute

HTML:

<div id="users">
    <div class="privateMessage" data-selected="" data-who="John">John</div>
    <div class="privateMessage" data-selected="" data-who="Maria">Maria</div>
    <div class="privateMessage" data-selected="" data-who="Tony">Tony</div>
</div>
<div id="messageBox"></div>

脚本:

$("#users").on("click", '.privateMessage', function () {
    if(!$(this).data('selected')){
        $(this).data('selected', 'selected');
        // do not use '.attr()', use natvie jQuery '.data()'
        var $msgTo = $(this).data('who');
        $("#messageBox").append("<div class='private-chat' data-conversation-between=" + $msgTo + ">"+$msgTo+"</div>");
    }
});

DEMO


或者,您可以只使用 .one() 事件,稍后为特定按钮重新激活它(f.ex。从聊天中删除此人后):

function singleClick(el) {
    $(el).one("click", function () {
        var $msgTo = $(this).data('who');
        $("<div class='private-chat' data-conversation-between=" + $msgTo + ">"+$msgTo+"</div>").appendTo("#messageBox");
    });
}

singleClick('.privateMessage');

DEMO (with delete example using .one())

希望它能达到您的预期。可以在附加 div 之前检查数据属性。

$('.privateMessage').on('click', function(e) {
  var isPresent = false;
  var whoIsIt = $(this).attr('data');
  $('#messageBox .private-chat').each(function(index, element) {
    if ($(this).attr('data-conversation-between') == whoIsIt) {
      isPresent = true;
    }
  });
  if (!isPresent) {
    $('#messageBox').append('<div class="private-chat" data-conversation-between="' + whoIsIt + '"></div>');
  }
});
.private-chat {
  height: 20px;
  background: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>


<div id="users">
  <div class="privateMessage" data="John">John</div>
  <div class="privateMessage" data="Maria">Maria</div>
  <div class="privateMessage" data="Tony">Tony</div>
</div>
<div id="messageBox"></div>