Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在用户输入时从HTML中提取名称@_Javascript_Jquery_Html_Css - Fatal编程技术网

Javascript 如何在用户输入时从HTML中提取名称@

Javascript 如何在用户输入时从HTML中提取名称@,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我有这个HTML结构: <table id="table-15244" cellspacing="0" class="comment"><tbody> <tr id="comment-37"> <td style="color: #999" class="NV"> </td> <td class="hidden-mob"> <a>&l

我有这个HTML结构:

<table id="table-15244" cellspacing="0" class="comment"><tbody>
    <tr id="comment-37">
        <td style="color: #999" class="NV">
        </td>
        <td class="hidden-mob">
            <a><i class="fa fa-caret-up c-voteup"></i></a><br>
            <a><i class="fa fa-bug c-bug"></i></a>
        </td>
        <td class="CT">This is first comment
            <samp>ــ</samp>
            <span>
                <a>Jack</a>                                      // Here
            </span>
            <span class="date_time">
                <span></span>
            </span>
        </td>
    </tr>

    <tr id="comment-37">
        <td style="color: #999" class="NV">
        </td>
        <td class="hidden-mob">
            <a><i class="fa fa-caret-up c-voteup"></i></a><br>
            <a><i class="fa fa-bug c-bug"></i></a>
        </td>
        <td class="CT">This is second comment
            <samp>ــ</samp>
            <span>
                <a>Peter</a>                                      // Here
            </span>
            <span class="date_time">
                <span></span>
            </span>
        </td>
    </tr>
</tbody></table>
JS:

// the above HTML is here
<textarea class="comment" rows="4" cols="50"></textarea>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>

$(".comment").on('keydown', function(e){
    if (e.which == 50) {  // @ pressed

        /* wait for first character that will be entered
         * if it is match with one or more of those names in the html
         * then show that name as a pop-up on the top of textarea
        */
    }
});

$(“.comment”)。关于('keydown',函数(e){
如果(e.which==50){/@按下
/*等待将输入的第一个字符
*如果与html中的一个或多个名称匹配
*然后将该名称显示为文本区域顶部的弹出窗口
*/
}
});

将类添加到包含名称的两个元素(或具有名称的任何其他元素)中,例如Peter

并使用jquery获取javascript中的所有名称

 el_names = $('.name');
 names = [];
 for (var i = 0; i < el_names.length;i++){t.push(el_names[i].text)}
el_names=$('.name');
名称=[];
对于(var i=0;i
然后,您可以在keydown处理程序中使用一个名称数组


另外,为了在键入时显示匹配的名称,您可以使用这样的库(或您在网上找到的任何其他更好的库)

这个库怎么样:
twitter-typeahead
?在HTML中给
Peter
一个id(
Peter
),并通过
$(“#myID.text”)在JS中获取其值
?您的html代码库让人看起来很痛苦,我建议您放弃所有内容,使用jquery的Autocomplete解决方案