通过添加span来突出显示搜索词?

时间:2015-02-27 02:19:18

标签: javascript

目前尝试突出显示搜索字词&#34;发现在&#34;表&#34;通过发现&#34;找到的结果&#34;进入<span>。我收到错误#34;意外字符串&#34;没有结果。有人可以看一看并告诉我我做错了吗?

&#13;
&#13;
//string are words in table
var str = ("table#id tr td");
//regexp is the word entered in input field
var re = $("#search-criteria");

var found = str.match(re);
console.log(found);

if (found !== "")
{
    //surround word found with span
    $(found).appendTo($"<span class="s_val">");}
else
{
    //remove all span.s_val if not found
    $("table#id tr td").children(.s_val).remove;
}
&#13;
span.s_val{
    background-color:yellow;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div>
    <input type="text" id="search-criteria" />
</div><hr>
<table id="test">
    <tr>
        <td>apple</td>
        <td>banana</td>
        <td>red</td>
        <td>green</td>
        <td>ee</td>
        <td>ana</td>
    </tr>
</table>
&#13;
&#13;
&#13;

0 个答案:

没有答案