简体   繁体   English

如何在jQuery中从文本输入中突出显示段落

[英]How can I highlight paragraphs from text input in jquery

I'm starting to learn jQuery today. 我今天开始学习jQuery I'm currently on 24th tutorial PHPachademy . 我目前在第24篇教程PHPachademy上 I'm having a lot of problems getting this particular tutorial to work correctly. 我在使此特定教程正常工作时遇到很多问题。 I'm pretty sure it's a semantic error >_<;; 我很确定这是一个语义错误> _ <;;

Heres an Attempt JSfiddles 继承人尝试JSfiddles

Lastly could someone explain why there this line to me? 最后有人可以向我解释为什么要这样做吗?

('" + search_name + "')

why are there so many + 's and '" in the line? 为什么行中有这么多的+'"

$('#names li').removeClass('highlight');

Instead of 代替

$(#names li).removeClass('highlight');

You reference the #names variable instead of using the '#names li' string 您引用#names变量,而不使用'#names li'字符串

Fixed fiddle 固定小提琴

Lastly could someone explain why there this line to me? 最后有人可以向我解释为什么要这样做吗? ('" + search_name + "') ('“ + search_name +”')

:contains selector docs : :contains选择器文档

Description: Select all elements that contain the specified text. 说明:选择包含指定文本的所有元素。 The matching text can appear directly within the selected element, in any of that element's descendants, or a combination thereof. 匹配的文本可以直接出现在所选元素中,该元素的任何后代或其组合中。 As with attribute value selectors, text inside the parentheses of :contains() can be written as a bare word or surrounded by quotation marks. 与属性值选择器一样, :contains()括号内的文本可以写为裸词,也可以用引号引起来。 The text must have matching case to be selected. 文本必须具有匹配的大小写才能选择。

Fiddle without the quotation marks 无引号的小提琴

By the way, since you're trying to learn jQuery 顺便说一句,由于您正在尝试学习jQuery

An important rule about jQuery is, jQuery shouldn't can can't replace javascript! 关于jQuery的一个重要规则是,jQuery不能不能取代javascript! it's an abstraction library above javascript. 这是javascript之上的抽象库。 Don't over use it, this.value can bring you the value of input a lot easier and faster then $(this).val() . 不要过度使用它,与$(this).val()this.value可以为您带来输入值的便捷$(this).val()

You can read more here 你可以在这里阅读更多

just add " 只需添加“

$("#names li")

http://jsfiddle.net/Hg72b/7/ http://jsfiddle.net/Hg72b/7/

在此处输入图片说明

When referencing something in the dom jquery always parse a string in the form of 当引用dom jquery中的内容时,始终以以下形式解析字符串:

$('somethingtosearchfor').doSomethingOnIt()

In your case, you are building a jquery line from a search_name variable so you have to do a concatenation (+) between strings that are delimited by ". With this concatenation, you are building the jquery syntax to search in the dom and this syntax must be a string so you concatenate ' chars with your search name to have as a result the same syntax as gdoron mention 在您的情况下,您正在从search_name变量构建一个jquery行,因此必须在以“分隔的字符串之间执行串联(+)。通过此串联,您正在构建jquery语法以在dom和此语法中进行搜索必须是一个字符串,这样您才能将'chars与您的搜索名称连接起来,使其语法与gdoron提及相同

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM