[jQuery-fr] rechercher un mot et le mettre en apparence

420 views
Skip to first unread message

Pascal

unread,
Mar 13, 2011, 12:21:34 PM3/13/11
to jQue...@rezo.net
Bonjour à tous,

Je souhaiterais savoir s'il était possible de rechercher dans un texte
un mot clé et de colorer toutes les occurrences en javascript.

Je pense que cela doit-être faisable mais je ne vois pas comment.

Des idées?
Merci d'avance

Pascal
_______________________________________________
jQue...@rezo.net - http://listes.rezo.net/mailman/listinfo/jquery-fr

Michel L'HUILLIER

unread,
Mar 13, 2011, 2:46:26 PM3/13/11
to jque...@rezo.net
Bonsoir,

C'est du JS pur, pas du jQuery - mais ça rend service :

Le JS :

function doHighlight(bodyText, searchTerm, highlightStartTag,
highlightEndTag)
{
if ((!highlightStartTag) || (!highlightEndTag)) {
highlightStartTag = "<font style='color:blue;
background-color:yellow;'>";
highlightEndTag = "</font>";
}
var newText = "";
var i = -1;
var lcSearchTerm = searchTerm.toLowerCase();
var lcBodyText = bodyText.toLowerCase();
while (bodyText.length > 0) {
i = lcBodyText.indexOf(lcSearchTerm, i+1);
if (i < 0) {
newText += bodyText;
bodyText = "";
} else {
if (bodyText.lastIndexOf(">", i) >= bodyText.lastIndexOf("<", i)) {
if (lcBodyText.lastIndexOf("/script>", i) >=
lcBodyText.lastIndexOf("<script", i)) {
newText += bodyText.substring(0, i) + highlightStartTag +
bodyText.substr(i, searchTerm.length) + highlightEndTag;
bodyText = bodyText.substr(i + searchTerm.length);
lcBodyText = bodyText.toLowerCase();
i = -1;
}
}
}
}

return newText;
}
function highlightSearchTerms(searchText, treatAsPhrase, warnOnFailure,
highlightStartTag, highlightEndTag)
{
if (treatAsPhrase) {
searchArray = [searchText];
} else {
searchArray = searchText.split(" ");
}

if (!document.body || typeof(document.body.innerHTML) == "undefined") {
if (warnOnFailure) {
alert("Sorry, for some reason the text of this page is
unavailable. Searching will not work.");
}
return false;
}

var bodyText = document.body.innerHTML;
for (var i = 0; i < searchArray.length; i++) {
bodyText = doHighlight(bodyText, searchArray[i], highlightStartTag,
highlightEndTag);
}

document.body.innerHTML = bodyText;
return true;
}


Et la commande dans le HTML :

&lt;bodyonload="highlightSearchTerms('mot_que_tu_veux_surligner');"&gt;


Michel

Michel L'HUILLIER

unread,
Mar 13, 2011, 2:49:34 PM3/13/11
to jque...@rezo.net
L'exemple du résultat ici :

http://mlh02.free.fr/mediatheque/__essai_body_text.php

Le 13/03/2011 17:21, Pascal a écrit :

Herve Thouzard

unread,
Mar 15, 2011, 1:46:31 PM3/15/11
to Michel L'HUILLIER, jque...@rezo.net
Ca marche aussi (je ne casse pas la page) si je fais une recherche sur par exemple <a href ?

mlh

unread,
Mar 15, 2011, 1:50:41 PM3/15/11
to jque...@rezo.net
Il s'agit d'une recherche "TEXTE". Pas HTML.

Herve Thouzard

unread,
Mar 15, 2011, 2:29:29 PM3/15/11
to mlh, jque...@rezo.net
J'ai bien compris....
Mais que se passe t'il si je recherche un TEXTE qui pourrait être du code html.

Michel L'HUILLIER

unread,
Mar 15, 2011, 3:53:40 PM3/15/11
to jque...@rezo.net
?

développe...

Morin Lionel

unread,
Mar 16, 2011, 5:43:13 AM3/16/11
to jque...@rezo.net
Bonjour

A une époque je me suis servi d'un plugin pour surligner certain
mot de ma page.
Peut-être te conviendra-t-il :
http://www.jquery.info/spip.php?article50.

hth
--
Lionel Morin

dj G-traxx

unread,
Mar 17, 2011, 8:47:06 AM3/17/11
to jquery-fr
Je vous conseille ce plugin : http://bartaz.github.com/sandbox.js/jquery.highlight.html
Facile à installé et tellement simple à configurer :)

2011/3/16 Morin Lionel <lionel...@cned.fr>
Reply all
Reply to author
Forward
0 new messages