I found a JQuery Script which allows you to highlight a word in a text by clicking on it.
(我发现了一个JQuery脚本,它允许您通过单击来突出显示文本中的单词。)
Anyhow, is it possible to adapt the code that it is only possible to highlight one word by clicking on it?
(无论如何,是否可以改编只能通过单击突出显示一个单词的代码?)
So, if the user clicks on a second word, the first color coded word would go back to normal.(因此,如果用户单击第二个单词,则第一个用颜色编码的单词将恢复正常。)
var words = $( "p" ).first().text().split( /\s+/ ); var text = words.join( "</span> <span>" ); $( "p" ).first().html( "<span>" + text + "</span>" ); $( "span" ).on( "click", function() { $( this ).css( "background-color", "red" ); });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <!DOCTYPE html> <html> <head> <script src="https://code.jquery.com/jquery-git.js"></script> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <title>Click a word in the paragraph and highlight it.</title> </head> <body> <p> This domain is established to be used for illustrative examples in documents. You may use this domain in examples without prior coordination or asking for permission. </p> </body> </html>
Unfortunately I cannot find the original source anymore from stackoverflow.
(不幸的是,我无法从stackoverflow中找到原始来源。)
ask by InPanic translate from so
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…