[jQuery Note]jQuery Control your wrapped set

The previous article records some CSS-Selector and Custom jQuery-Selector.

Today,let's talk about how to control your wrapped set.
 
$('#someDiv').html('There are '+$('a').size()+' link(s) in the div named someDiv ');
 
function or code description example
size() return the count of elements in the wrapped set $('input').size()
hide() make the element(s) unvisible $('#someDiv').hide()
get(index) Obtains one or all(if index not be provided) mached in elements in the wrapped set. example1
eq()    
first() Obtains the first element in the wrapped set and return a new wrapped set containing just that element.  
last()    
index(element) Finds the passed element in the wrapped set and returns its ordinal index within the set.
element can be short by using selector
$('img').index($('img#findme')[0]);
shorten style:
$('img').index('img#findme')
add(expression) creates a copy of the wrapped set and adds elements,specified by the expression parameter,to the new set.   
not(expression)    
filter(expression) creates a copy of the wrapped set and removes elements from the new set that don't match criteria specified by the value of the expression parameter.  
has(test)    
slice(begin,end)   $('div').has('img[alt]')
map(callback function)    
each(iterator) Traverses all elements in the matched set,invoking the passed iterator function for each. $('img').each(function(n){
 this.alt='this is image['+n+'] with an id of '+this.id;
});
 
example1
Because jQuery allows us to treate the wrapped set as a JavaScript array,we can simple array indexing to obtain any element in the wrapped list by position.
for example: 
     To obtain the firsst element in the set of all <img> element with an alt attribution on the page,we can write :
     var imgElement =$('img[alt]')[0]; equals $('img[alt]').get(0)
ok,let's review the previous artical.we can also write:
     var imgElement =$('img[alt]:first') 
the difference between them is that the latter one will create a new wrapped  set that contains the just element.

你可能感兴趣的:(jquery)