Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
322 views
in Technique[技术] by (71.8m points)

javascript - 通过单击突出显示一个单词(highlighting one word by clicking on it)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

First call $("span").css("background-color", "");

(首先调用$("span").css("background-color", "");)

to reset the color of all <span> s before setting the color of the clicked element:

(在设置单击元素的颜色之前重置所有<span>的颜色:)

 var words = $("p").first().text().split(/\s+/); var text = words.join("</span> <span>"); $("p").first().html("<span>" + text + "</span>"); $("span").on("click", function() { $("span").css("background-color", ""); $(this).css("background-color", "red"); }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <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> 

You can also save the clicked element in a variable:

(您也可以将clicked元素保存在变量中:)

 var words = $("p").first().text().split(/\s+/); var text = words.join("</span> <span>"); $("p").first().html("<span>" + text + "</span>"); let highlighted; $("span").on("click", function() { $(highlighted).css("background-color", ""); $(this).css("background-color", "red"); highlighted = this; }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <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> 


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...