Visualizzazione dei risultati da 1 a 2 su 2
  1. #1

    Eventi da tastiera su motore di ricerca JS

    Ciao a tutti!!!
    Ho una domanda. Ho uno script che mi permette di cercare ed evidenziare una parola all'interno di una pagina web. Ad esempio se cerco Development, mi evidenzia tutte le occorrenze di "development" in questa pagina.

    Vorrei realizzare 2 cose:
    1. sottolineare un'occorrenza alla volta
    2. spostarmi da un'occorrenza all'altra tramite tastiera.

    Come si fa?

    Vi lascio il mio codice:


    codice:
    function eventoonloadbody () {
    
    var body=document.getElementsByTagName('body')[0];
    var buttons = '<button onclick="Search();">Search</button>';
    body.innerHTML=buttons+body.innerHTML;
    
    }
    
    function Search(){
    	var body=document.getElementsByTagName('body')[0];
    	search_item (body);
    }
    
    
    
    function search_item (node) {
    
    var cible="development";
        if (node.nodeType == 3) {
    	
    		var parent_node =node.parentNode;
    		var content_node = node.nodeValue;
    		var pos_node = content_node.indexOf(cible);
    
    		while (pos_node >=0) { 
    	   
    			var left= content_node.substr(0,pos_node);
    			var right= content_node.substr(pos_node+cible.length);
    			var span= document.createElement('span');
    			var start= document.createTextNode(left);
    		
    			var word=document.createTextNode(cible);
    			span.appendChild(word);
    			parent_node.insertBefore(start,node);
    			parent_node.insertBefore(span,node);
    			
    			content_node= right;
    			pos_node = content_node.indexOf(cible);
    			
    		
    			 span.style.backgroundColor='#CDDE47';
    			 span.style.color='#E8582E';
    		}
    	
    		var end = document.createTextNode(content_node);
    		parent_node.insertBefore(end,node);
    		node.remove();
    	
        } else {
    	
    		var children = node.childNodes;
    		var child = new Array();
    		for (var i = 0; i<children.length; i++) {
    		
    			child[i] = children[i];
    		}
    		for (var i = 0; i < child.length; i++) {
    		
    			search_item(child[i]);
    		}	
        }
    }

  2. #2
    [I]Enigma risolto da me, a chi servisse ecco il codice.

    codice:
    function initialise_extraction () {
    
    var body=document.getElementsByTagName('body')[0];
    var buttons = '<button onclick="Search();">Search</button>';
    body.innerHTML=buttons+body.innerHTML;
    
    }
    
    function Search(){
        var body=document.getElementsByTagName('body')[0];
        search_item (body);
    }
    
    
    function showFocus(elem) {
          document.getElementById('focus').innerHTML = "focus "+elem.tabIndex
        }
    
    function search_item (node) {
    
    var cible="Master";
        if (node.nodeType == 3) {
        
            var parent_node =node.parentNode;
            var content_node = node.nodeValue;
            var pos_node = content_node.indexOf(cible);
        //alert(pos_node);
            while (pos_node >=0) { 
    
                
                
                var left= content_node.substr(0,pos_node);
                var right= content_node.substr(pos_node+cible.length);
                var span= document.createElement('span');
                
                var elt_a= document.createElement('a');
                
                var att = document.createAttribute("tabindex");
                att.value = '"1"';
                elt_a.setAttributeNode(att);
                var att2 = document.createAttribute("onfocus");
                att2.value = 'showFocus(this)';
                elt_a.setAttributeNode(att2);
                
                var att3 = document.createAttribute("onblur");
                att3.value = 'showBlur(this)';
                elt_a.setAttributeNode(att3);
            
                    
                var start= document.createTextNode(left);
                //var end= document.createTextNode(right);
                var word=document.createTextNode(cible);
                
                
                span.appendChild(elt_a);
                elt_a.appendChild(word);
    
                parent_node.insertBefore(start,node);
                parent_node.insertBefore(span,node);
                
                content_node= right;
                pos_node = content_node.indexOf(cible);
                
         
                 
                 
            }
        
            var end = document.createTextNode(content_node);
            parent_node.insertBefore(end,node);
            node.remove();
        
        } else {
        
            var children = node.childNodes;
            var enfants = new Array();
            for (var i = 0; i<children.length; i++) {
            
                enfants[i] = children[i];
            }
            for (var i = 0; i < enfants.length; i++) {
            
                search_item(enfants);
            }    
        }
    }
    
    function showFocus(elem) {
        var color = document.createAttribute('style');
        color.value = "background-color : #CDDE47";
        elem.setAttributeNode(color);
        }
        
    function showBlur(elem) {
        elem.style.backgroundColor = 'white';
        }
    
    


Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.