2011-09-14 11 views
5

Quello che cercando:Come posso utilizzare il selettore jQuery-come in puro JavaScript

var arrinput = $('input[name$="letter"]') 

Come posso cambiare che da stile jQuery per uno stile javascript puro?

Quindi voglio i tag <input> che name terminano con "lettera".


ho cambiato il codice un po '... Il mio browser dont sostegno querySelector e FYI sto usando componente browser web su C# WinForms

+1

Perché si vuole a? – meagar

+2

coz Non voglio includere jQuery.js nella mia pagina :) –

+1

Perché non vuoi? – meagar

risposta

3

Prova:

var items = document.getElementsByTagName('input'); 
for (var i=0; i<items.length; i++) { 
    var item = items[i]; 
    if (/letter$/.test(item.name)) { 
     item.value = "A letter"; 
    } 
} 
+1

Quasi, ma 'for (elemento in input)' non funziona in questo modo. 'item' sarà un indice. È necessario 'for (i in input) {var item = inputs [i]; ...} 'o meglio ancora,' for (var i = 0; i meagar

+0

Whoops! Grazie per quello. L'ho dimenticato - ho appena lavorato con Python: D – BenjaminRH

+0

if (/letter$/.test(item.name)) << - che cosa significa? Non capisco :( –

9

per i browser moderni:

document.querySelector('input[name$=letter]');

restituirà la prima corrispondenza.

document.querySelectorAll('input[name$=letter]');

restituirà un elenco di partite.

Ho il sospetto che se si guarda attraverso il codice sorgente jQuery, utilizza document.querySelector[All] quando è disponibile.

+0

Stavo per rispondere. Troppo tardi ... –

+1

troppo male il mio browser non supporta querySelector :(FYI utilizzo il componente webbrowser su C# winforms –

1

È un vecchio post, ma ho cercato una soluzione simile e non l'ho trovato.

così ho a fare un po 'di funzione per fare che (è ottimizzabile):

/** 
* Searches and finds the parent node for a dom object 
* 
* @examples: 
* getParent(elem, 'div')     // The first div parent found 
* getParent(elem, 'div[id]')    // The first div parent with an id found 
* getParent(elem, 'div[id="toto"]')  // The first div parent with id equals toto found 
* getParent(elem, 'div[id=^="toto"]')  // The first div parent with id start by toto found 
* getParent(elem, 'div[id=$="toto"]')  // The first div parent with id end by toto found 
* getParent(elem, 'div[id=*="toto"]')  // The first div parent with id contains toto found 
* 
* @param domObject elem 
* @param string [target] 
* @return domObject or null 
*/ 
function getParent(elem, target) 
{ 
    if(target == null) 
     return elem.parentNode; 

    var elem_name = target, 
     attr_name = null, attr_value = null, 
     compare_type = null, 
     match_val = target.match(/\[.+[^\[\]]\]$/i); 

    if(match_val != null) 
    { 
     elem_name = elem_name.replace(match_val[0], ''); 

     var expr = match_val[0].substr(1, match_val[0].length-2), 
      tmp = expr.split('='); 

     attr_name = tmp[0]; 
     if(tmp.length == 2) 
     { 
      attr_value = tmp[1].toLowerCase(); 
      attr_value = attr_value.replace(/(\'|\")+/ig, ''); 

      if(attr_name.match(/\^$/)) 
       compare_type = 'begin'; 
      else if(attr_name.match(/\*$/)) 
       compare_type = 'all'; 
      else if(attr_name.match(/\$$/)) 
       compare_type = 'end'; 
      else 
       compare_type = 'simple'; 

      if(compare_type != 'simple') 
       attr_name = attr_name.substr(0, attr_name.length-1); 
     } 
    } 

    var parent = elem.parentNode; 

    do 
    { 
     if(parent.nodeName.toUpperCase() == elem_name.toUpperCase()) 
     { 
      if(attr_name != null) 
      { 
       var attribute = parent.getAttribute(attr_name).toLowerCase(); 
       if(attribute != null && attribute != '') 
       { 
        if(attr_value == null) 
         return parent; 

        if(compare_type == 'simple' && attribute == attr_value) 
         return parent; 
        if(compare_type == 'begin' && attribute.match(eval('/^'+attr_value+'/ig'))) 
         return parent; 
        if(compare_type == 'end' && attribute.match(eval('/'+attr_value+'$/ig'))) 
         return parent; 
        if(compare_type == 'all' && attribute.match(eval('/'+attr_value+'/ig'))) 
         return parent; 
       } 
      } else { 
       return parent; 
      } 
     } 

     parent = parent.parentNode; 
    } 
    while(parent != null); 

    return null; 
} 
Problemi correlati