[英]Use JQuery to highlight cell when text is typed into a text input
I'm trying to highlight table cells as text is typed into an input. 我正在尝试突出显示表格单元格,因为在输入中输入了文本。
My code is: 我的代码是:
$(document).ready(function() {
$('#search').keyup(function() {
var value = $('#search').val();
$('td:contains(value)').css({'border-color':'red'});
});
});
I don't understand why this is not working, please help me understand! 我不明白为什么这行不通,请帮助我理解!
EDIT: #search is a text input where the search term is entered. 编辑:#search是输入搜索词的文本输入。
You're searching for the string value
, not for the contents of the variable value
. 您正在搜索字符串
value
,而不是变量value
的内容。 Use concatenation to put the variable's value into your selector: 使用串联将变量的值放入选择器:
$('td:contains(' + value + ')').css({'border-color':'red'});
Edit: 编辑:
You also need to set the other border properties: 您还需要设置其他边框属性:
$('td:contains(' + value + ')').css({border: 'red 1px solid'});
Note also that this has fairly odd behaviour. 还请注意,这具有相当奇怪的行为。 The moment I type a letter into the input box, this code will highlight every
td
that contains that letter. 当我在输入框中输入字母时,此代码将突出显示包含该字母的每个
td
。 Deleting the contents of the input has no effect. 删除输入内容无效。 If you want a different effect, you'll have to say what it is.
如果您想要不同的效果,则必须说出它的含义。 Whole words, perhaps?
整个词,也许?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.