jQuery在不同div的点击后附加到div

时间:2015-02-22 17:28:58

标签: javascript jquery append

我有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>

现在,当我点击用户时,我正在努力在div内动态附加messageBox

我需要的是以下内容:

<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解决这个问题吗?

链接:fiddle

5 个答案:

答案 0 :(得分:1)

这样的事情怎么样?

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

click事件已委派,因为可以动态地将用户添加到列表中。我还在messageBox搜索该用户的现有div,以便不添加其他用户。

在这里添加代码,不仅仅是链接到小提琴:

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();
});

答案 1 :(得分:1)

在评论中做出简短澄清之后,我发布了一个可行的解决方案:

$('.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),如果没有,请将其附加到那里。

答案 2 :(得分:1)

这个怎么样?

$('.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()。

答案 3 :(得分:1)

您应该避免以这种方式使用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())

答案 4 :(得分:1)

希望它能满足您的期望。可以在追加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>