Home >Web Front-end >JS Tutorial >Use jQuery to manage selection results_jquery
The elements selected using jQuery are very similar to arrays. They can be processed through a series of methods provided by jQuery, including length, finding an element, intercepting a paragraph, etc.
1. Get the number of elements.
In jQuery, you can get the number of elements in the selector through the size() method, which is similar to the length attribute in an array and returns an integer value, for example:
$("img").size()
Get the number of all images on the page
The following is an example of adding div blocks by continuously clicking and calculating the
上面代码将页面本身的6个
get(index)方法可以获取指定位置的元素,反过来,index(element)方法可以查找元素的element所处的位置。例如
var iNum=$("li").index($(li[title=isaac]")[0])
以上取
例:用index()方法获取元素的序号
以上的Jquery通过not()的方法去掉风格为"green"和"blueone"的
not()方法所接收的参数都不能包含特定的元素,只能是通过通用的表达式例如下面的代码是错误的
$("li[title]").not("img[title*=isaac]")
正确的写法是:
$("li[tile]").not("[title*=isaac]")
除了add()和not()外,jQuery还提供了更强大的filter()方法来筛选元素。filter()可以接受两种类型的参数,一种与not()方法一样,接受通用的表达式。代码如下:
$("li").filter("[title*=isaac]")
以上的代码表示:筛选出title值包含isaac字符串的li元素组合。
与
$("li[title*=isaac]")
所筛选的组合相同。
In the above code, four of the class attributes are middle. Jq first adds css1 styles to all div blocks, and then uses the filter() method to add css2 styles to the divs containing middle in the class.
In the parameters of filter(), you cannot directly equal to match (=), you can only use pre-match (^=), post-match (&=), or any match (*=).
The other type of parameter of filter() is a function, which matches and retains the returned true element, otherwise the collection is excluded. Function parameters are very powerful and allow users to customize filtering functions.
For example: