【问题标题】:How to show commented user's list when user key in @ key textarea field当用户在@ key textarea字段中键入时如何显示评论用户的列表
【发布时间】:2019-06-12 15:28:01
【问题描述】:

当用户在回复 Messanger/Messages 中的任何特定消息时在@keyin textarea 字段中键入时如何显示评论用户的列表

【问题讨论】:

    标签: php chat messaging


    【解决方案1】:

    我写这个 jquery 代码来获取列表

    
    $(document).on('keypress', '#comment_input', function(event){
        if(event.which == 64) {
    
            var elems = document.getElementsByClassName("usrcmt__name");
            var arr = jQuery.makeArray(elems);
            var namelist = {};
            var namelisttag ='<ul>';
    
            $.each(arr, function(index, value) {
                namelist[value.innerText] = value.innerText;
            });
    
            $.each(namelist, function(index, name) {
                namelisttag+='<li>'+name+'</li>';
            });
            namelisttag+='</ul></div>';
    
            $('.tribute-container').css({'top':'5px','left':'10px','position':'absolute','z-index':'9999','display':'block'}).html(namelisttag);    
        }
    
    }); //reply approve end
    
    //when click on delete or backspace btn delete list
    $(document).on('keydown', '#comment_input', function(event){
         var key = event.keyCode || event.charCode;
        if(key == 8 || key == 46) {
            $('.tribute-container ul').remove();    
        }
    }); 
    
    //when click on user , append user name in text area
    $(document).on('click', '.tribute-container ul li', function(event){
        var currentVal = $('#comment_input').val();
        $('#comment_input').val(currentVal +$(this).text()+' '); 
        $('.tribute-container ul').remove();
    }); 
    
    

    ================================================ ==============

    html代码

    <textarea id="comment_input"></textarea>
    <div class="tribute-container"></div>
    
    <div class="comments_list">
      <div class="comment">
        <div class="usrcmt__name"> users1 users1 </div>
        <div>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,</div>
      </div>
      <div class="comment">
        <div class="usrcmt__name"> users1 users1 </div>
        <div>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,</div>
      </div>
      <div class="comment">
        <div class="usrcmt__name"> users2 users2 </div>
        <div>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,</div>
      </div>
      <div class="comment">
        <div class="usrcmt__name"> users1 users1 </div>
        <div>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,</div>
      </div>
      <div class="comment">
        <div class="usrcmt__name"> users3 users3 </div>
        <div>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,</div>
      </div>
      <div class="comment">
        <div class="usrcmt__name"> users2 users2 </div>
        <div>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,</div>
      </div>
    </div>
    
    

    ================================================ ===== style.css 一些样式

    .tribute-container li{
      list-style:none;
      cursor:pointer;
    }
    
    .tribute-container li:hover{
      background:#328af1;
      color:#fff;
      cursor:pointer;
    }
    
    textarea {
        margin-bottom:40px;
    }
    
    Check this link for your answer  [here](https://jsfiddle.net/DineshBW/zun7avLs "jsfiddle link")
    
    

    【讨论】:

    • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接答案可能会失效。 - From Review
    • @sylvanaar 现在可以了吗?
    猜你喜欢
    • 2020-12-11
    • 1970-01-01
    • 2020-12-04
    • 1970-01-01
    • 1970-01-01
    • 2012-12-07
    • 2015-08-27
    • 2023-03-17
    • 1970-01-01
    相关资源
    最近更新 更多