IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

jQuery Discussion :

Rechercher un mot dans une page et aller directement au mot


Sujet :

jQuery

  1. #1
    Membre habitué
    Inscrit en
    Janvier 2007
    Messages
    437
    Détails du profil
    Informations forums :
    Inscription : Janvier 2007
    Messages : 437
    Points : 184
    Points
    184
    Par défaut Rechercher un mot dans une page et aller directement au mot
    Bonjour à tous,

    je vous écris ce jour car j'essaye de créer une fonction qui permet de rechercher un mot et y aller directement à la ligne du monde

    la même fonction que firefox sur "Rechercher sur la page" est t'il possible de réaliser ce script en jquery ?

    cordialement

  2. #2
    Expert confirmé Avatar de Toufik83
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Janvier 2012
    Messages
    2 414
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : Suisse

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Janvier 2012
    Messages : 2 414
    Points : 4 864
    Points
    4 864
    Par défaut
    oui c'est bien possible en utilisant indexOf(), et pour défiler la page on utilise scrollTo().

  3. #3
    Rédacteur/Modérateur

    Avatar de SpaceFrog
    Homme Profil pro
    Développeur Web Php Mysql Html Javascript CSS Apache - Intégrateur - Bidouilleur SharePoint
    Inscrit en
    Mars 2002
    Messages
    39 638
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 74
    Localisation : Royaume-Uni

    Informations professionnelles :
    Activité : Développeur Web Php Mysql Html Javascript CSS Apache - Intégrateur - Bidouilleur SharePoint
    Secteur : Industrie

    Informations forums :
    Inscription : Mars 2002
    Messages : 39 638
    Points : 66 668
    Points
    66 668
    Billets dans le blog
    1
    Par défaut
    sauf que scrollTo sur quoi ??

    il va falloir replacer l'occurrence du texte pour placer une balise avec une ancre pour arriver à y scroller...
    Ma page Developpez - Mon Blog Developpez
    Président du CCMPTP (Comité Contre le Mot "Problème" dans les Titres de Posts)
    Deux règles du succès: 1) Ne communiquez jamais à quelqu'un tout votre savoir...
    Votre post est résolu ? Alors n'oubliez pas le Tag

    Venez sur le Chat de Développez !

  4. #4
    Membre habitué
    Inscrit en
    Janvier 2007
    Messages
    437
    Détails du profil
    Informations forums :
    Inscription : Janvier 2007
    Messages : 437
    Points : 184
    Points
    184
    Par défaut
    désolé du temps de réponse je n'étais pas revenu sur le forum,

    est t'il possible sinon au click de simuler le ctrl f ?

  5. #5
    Membre habitué
    Inscrit en
    Janvier 2007
    Messages
    437
    Détails du profil
    Informations forums :
    Inscription : Janvier 2007
    Messages : 437
    Points : 184
    Points
    184
    Par défaut
    j'ai pu trouver un script que j'ai legerement modifier pour qu'il correspond à mes attentes,

    sur le pc ça fonctionne mais pas sur les mobiles vous aurez une idee ?


    Code html : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    <script>
      $(document).ready(function() {
    findString = function findText(text) {
      alert("String \x22" + text + "\x22 found? " + window.find(text));
    }
     
    $('#but').on('click', function() {
                    
                    var val = $("#search").val();
                    FindNext ();
                            
            });
     
    });     
      </script>
     
          <script type="text/javascript">
            function FindNext () {
                var str = document.getElementById ("findField").value;
                if (str == "") {
                    alert ("Please enter some text to search!");
                    return;
                }
     
                var supported = true;
                var found = false;
                if (window.find) {        // Firefox, Google Chrome, Safari
                    supported = false;
                        // if some content is selected, the start position of the search 
                        // will be the end position of the selection
                    found = window.find (str);
                }
                else {
                    if (document.selection && document.selection.createRange) { // Internet Explorer, Opera before version 10.5
                        var textRange = document.selection.createRange ();
                        if (textRange.findText) {   // Internet Explorer
                            supported = true;
                                // if some content is selected, the start position of the search 
                                // will be the position after the start position of the selection
                            if (textRange.text.length > 0) {
                                textRange.collapse (true);
                                textRange.move ("character", 1);
                            }
     
                            found = textRange.findText (str);
                            if (found) {
                                textRange.select ();
                            }
                        }
                    }
                }
     
               
            }
        </script>

  6. #6
    Expert éminent sénior
    Avatar de ProgElecT
    Homme Profil pro
    Retraité
    Inscrit en
    Décembre 2004
    Messages
    6 078
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 68
    Localisation : France, Haute Savoie (Rhône Alpes)

    Informations professionnelles :
    Activité : Retraité
    Secteur : Communication - Médias

    Informations forums :
    Inscription : Décembre 2004
    Messages : 6 078
    Points : 17 124
    Points
    17 124
    Par défaut
    Salut

    Et si tu mes tout entre les mêmes balises <script> .... </script> (sans préciser type="text/javascript")
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    <script>
      $(document).ready(function() {
    findString = function findText(text) {
      alert("String \x22" + text + "\x22 found? " + window.find(text));
    }
     
    $('#but').on('click', function() {
     
    		var val = $("#search").val();
    		FindNext ();
     
    	});
     
    });	
            function FindNext () {
                var str = document.getElementById ("findField").value;
                if (str == "") {
                    alert ("Please enter some text to search!");
                    return;
                }
     
                var supported = true;
                var found = false;
                if (window.find) {        // Firefox, Google Chrome, Safari
                    supported = false;
                        // if some content is selected, the start position of the search 
                        // will be the end position of the selection
                    found = window.find (str);
                }
                else {
                    if (document.selection && document.selection.createRange) { // Internet Explorer, Opera before version 10.5
                        var textRange = document.selection.createRange ();
                        if (textRange.findText) {   // Internet Explorer
                            supported = true;
                                // if some content is selected, the start position of the search 
                                // will be the position after the start position of the selection
                            if (textRange.text.length > 0) {
                                textRange.collapse (true);
                                textRange.move ("character", 1);
                            }
     
                            found = textRange.findText (str);
                            if (found) {
                                textRange.select ();
                            }
                        }
                    }
                }
     
     
            }
        </script>
    pourquoi pas, pour remercier, un pour celui/ceux qui vous ont dépannés.
    saut de ligne
    OOOOOOOOO👉 → → Ma page perso sur DVP ← ← 👈

  7. #7
    Membre habitué
    Inscrit en
    Janvier 2007
    Messages
    437
    Détails du profil
    Informations forums :
    Inscription : Janvier 2007
    Messages : 437
    Points : 184
    Points
    184
    Par défaut
    ça change rien

  8. #8
    Membre habitué
    Inscrit en
    Janvier 2007
    Messages
    437
    Détails du profil
    Informations forums :
    Inscription : Janvier 2007
    Messages : 437
    Points : 184
    Points
    184
    Par défaut
    Aprés multe recherche voici afin une solution qui marche aussi sur mobile :

    La fonction :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
     
          <script type="text/javascript">
            function FindNext () {
                var str = document.getElementById ("findField").value;
                if (str == "") {
                    alert ("Champs vide");
                    return;
                }
     
                var supported = true;
                var found = false;
                if (window.find) {        // Firefox, Google Chrome, Safari
                    supported = false;
                        // if some content is selected, the start position of the search 
                        // will be the end position of the selection
                    found = window.find (str);
                }
                else {
                    if (document.selection && document.selection.createRange) { // Internet Explorer, Opera before version 10.5
                        var textRange = document.selection.createRange ();
                        if (textRange.findText) {   // Internet Explorer
                            supported = true;
                                // if some content is selected, the start position of the search 
                                // will be the position after the start position of the selection
                            if (textRange.text.length > 0) {
                                textRange.collapse (true);
                                textRange.move ("character", 1);
                            }
     
                            found = textRange.findText (str);
                            if (found) {
                                textRange.select ();
                            }
                        }
                    }
                }
     
     
            }
        </script>

    L'appel de la fonction : je l'ai adapté en jquery :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    $('#but').on('click', function() {
     
    		var val = $("#search").val();
    		FindNext ();
     
    	});
     
    	$('#findField').on('focus', function() {
    		$("html, body").animate({scrollTop: 0},"slow");
     
    	});
     
    });
    et comme il n'y a pas la recherche vers le haut j'ai rajouté le script permettant de remonté en haut des que le curseur est dans le champs :



    et enfin voila le bouton :

    Code html : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    <input type="text" id="findField" value="" size="20" />
        <button id="but">Chercher</button>

  9. #9
    Membre régulier
    Profil pro
    Inscrit en
    Août 2010
    Messages
    176
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2010
    Messages : 176
    Points : 95
    Points
    95
    Par défaut Touches CTRL+F et recherche dans une page WEB
    Bonjour,

    Je suis débutante en HTML. J'ai une page Web dans lequel je veux aussi simuler les touches CTRL+F. EN lisant le code sur cette discussion,, je suis un peu perdu.
    Où doit -on insérer le code du "Script" et le code "jQuery". Pour le bouton en HTML, pas de problème.

    J'ai un fichier en HTML qui est relié à un fichier "CSS" seulement.

    Merci

  10. #10
    Membre habitué
    Inscrit en
    Janvier 2007
    Messages
    437
    Détails du profil
    Informations forums :
    Inscription : Janvier 2007
    Messages : 437
    Points : 184
    Points
    184
    Par défaut
    bonjour marycalou

    Pour inséré le code tu dois mettre le script entre les balises <head></head>

    le code tu dois aussi le mettre tel quel :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    <script>
     $(document).ready(function() {
     
    // ici tes scripts jquery et function js
     
     
    	});
     
    </script>
    et bien sur en haut de page les fichiers d'appelle a la librairie jquery pour que le code fonctionne
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    		<script  src="https://code.jquery.com/jquery-3.2.1.js"></script>
    <script  src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 0
    Dernier message: 05/08/2017, 14h59
  2. recherche d'un mot dans une page web
    Par fraisa1985 dans le forum Général JavaScript
    Réponses: 0
    Dernier message: 23/10/2008, 15h59
  3. Réponses: 1
    Dernier message: 17/08/2007, 15h19
  4. module de recherche de mots dans une page html
    Par Drozo dans le forum Général JavaScript
    Réponses: 5
    Dernier message: 01/08/2006, 09h57
  5. Recherche d'un mot dans une page
    Par Emcd dans le forum Langage
    Réponses: 4
    Dernier message: 12/01/2006, 18h25

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo