Foros del Web » Programando para Internet » Javascript »

Duda: Limitar busqueda a DIV

Estas en el tema de Duda: Limitar busqueda a DIV en el foro de Javascript en Foros del Web. Gente, He encontrado el siguiente codigo para buscar palabras en una página. Código: <!-- Copiar dentro del tag BODY --> <script language="JavaScript"> var NS4 = ...
  #1 (permalink)  
Antiguo 05/02/2010, 12:17
 
Fecha de Ingreso: mayo-2003
Mensajes: 312
Antigüedad: 20 años, 10 meses
Puntos: 2
Duda: Limitar busqueda a DIV

Gente,
He encontrado el siguiente codigo para buscar palabras en una página.

Código:
<!-- Copiar dentro del tag BODY -->

<script language="JavaScript">

var NS4 = (document.layers); 
var IE4 = (document.all);

var win = window;    // Con frames usar top.nombre.window;
var n   = 0;

function findInPage(str) {

  var txt, i, found;

  if (str == "")
    return false;

  // Find next occurance of the given string on the page, wrap around to the
  // start of the page if necessary.

  if (NS4) {

    // Look for match starting at the current point. If not found, rewind
    // back to the first match.

    if (!win.find(str))
      while(win.find(str, false, true))
        n++;
    else
      n++;

    // If not found in either direction, give message.

    if (n == 0)
      alert("Not found.");
  }

  if (IE4) {
    txt = win.document.body.createTextRange();

    // Find the nth match from the top of the page.

    for (i = 0; i <= n && (found = txt.findText(str)) != false; i++) {
      txt.moveStart("character", 1);
      txt.moveEnd("textedit");
    }

    // If found, mark it and scroll it into view.

    if (found) {
      txt.moveStart("character", -1);
      txt.findText(str);
      txt.select();
      txt.scrollIntoView();
      n++;
    }

    // Otherwise, start over at the top of the page and find first match.

    else {
      if (n > 0) {
        n = 0;
        findInPage(str);
      }

      // Not found anywhere, give message.

      else
        alert("Not found.");
    }
  }

  return false;
}

</script>

<form name="search" onSubmit="return findInPage(this.string.value);">
  <font size="2"><p></font><font size="3"><input name="string" type="text" size="15"
  onChange="n = 0;"></font> <input type="submit" value="Buscar"> </p>
</form>
Bien. El tema, es que me gustaría poder delimitar la busqueda a, por ejemplo, un div dentro de la página. Es decir, no quiero que busque en toda la página, sino, dentro de: <div id="buscar">.

Alguna sugerencia o forma de readaptacion?!
Mil gracias.
  #2 (permalink)  
Antiguo 05/02/2010, 12:27
Avatar de jackson666  
Fecha de Ingreso: noviembre-2009
Ubicación: Buenos Aires, Argentina
Mensajes: 1.971
Antigüedad: 14 años, 5 meses
Puntos: 65
Respuesta: Duda: Limitar busqueda a DIV

Te dejo una pista (pasale el mouse por arriba)

Código Javascript:
Ver original
  1. <div onmouseover="alert(this.innerHTML);">hola</div>
__________________
HV Studio
Diseño y desarrollo web
  #3 (permalink)  
Antiguo 05/02/2010, 12:59
 
Fecha de Ingreso: mayo-2003
Mensajes: 312
Antigüedad: 20 años, 10 meses
Puntos: 2
Respuesta: Duda: Limitar busqueda a DIV

Gracias por la pista, pero aún no logro dar en la tecla. Seguiré buscando.
  #4 (permalink)  
Antiguo 05/02/2010, 13:07
Avatar de jackson666  
Fecha de Ingreso: noviembre-2009
Ubicación: Buenos Aires, Argentina
Mensajes: 1.971
Antigüedad: 14 años, 5 meses
Puntos: 65
Respuesta: Duda: Limitar busqueda a DIV

Bueno, lo restante era solo imaginacion..
Tu quieres que las busquedas se limiten solo a los div's, verdad?
Y bueno, entonces solo busca en los div's...

Código Javascript:
Ver original
  1. function buscar(string){
  2.  
  3. var divs = document.getElementsByTagName('div');
  4.  
  5. for (var i in divs){
  6.  
  7. if(divs[i].innerHTML == string){
  8.  
  9. alert('Te encontre!');
  10.  
  11. }
  12.  
  13. }
  14.  
  15. }

Obviamente que podes tener texto dentro del div, en vez de una sola palabra, por lo que deberas usar expresiones regulares para hallarlo....
__________________
HV Studio
Diseño y desarrollo web
  #5 (permalink)  
Antiguo 05/02/2010, 13:18
 
Fecha de Ingreso: mayo-2003
Mensajes: 312
Antigüedad: 20 años, 10 meses
Puntos: 2
Respuesta: Duda: Limitar busqueda a DIV

Mil gracias. Vere de aplicarlo al ejemplo principal. Saludos!
  #6 (permalink)  
Antiguo 05/02/2010, 13:29
Avatar de jackson666  
Fecha de Ingreso: noviembre-2009
Ubicación: Buenos Aires, Argentina
Mensajes: 1.971
Antigüedad: 14 años, 5 meses
Puntos: 65
Respuesta: Duda: Limitar busqueda a DIV

Cita:
Iniciado por Dago Ver Mensaje
Mil gracias. Vere de aplicarlo al ejemplo principal. Saludos!
De nada! Un poco de karma no vendria mal, gracias!
__________________
HV Studio
Diseño y desarrollo web

Etiquetas: limitar, busquedas
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 17:45.