juqery 学习之四 筛选过滤

2020-05-23 06:26:19易采站长站整理


Checks the current selection against an expression and returns true, if at least one element of the selection fits the given expression.
If no element fits, or the expression is not valid, then the response will be ‘false’. ‘filter’ is used internally, therefore all rules that apply there apply here, as well.
返回值

Boolean


参数

expr (String) :用于筛选的表达式


示例

由于input元素的父元素是一个表单元素,所以返回true。


HTML 代码:


<form><input type=”checkbox” /></form>

jQuery 代码:


$(“input[type=’checkbox’]”).parent().is(“form”)

结果:


true
————————————————————————————————————–

map(callback)

将一组元素转换成其他数组(不论是否是元素数组)
你可以用这个函数来建立一个列表,不论是值、属性还是CSS样式,或者其他特别形式。这都可以用’$.map()’来方便的建立。


Translate a set of elements into another set of values (which may, or may not, be elements).
You could use this to build lists of values, attributes, css values – or even perform special, custom, selector transformations. This is provided as a convenience method for using ‘$.map()’.
返回值

jQuery


参数

callback (Function) : 给每个元素执行的函数


示例

把form中的每个input元素的值建立一个列表。


HTML 代码:


<p><b>Values: </b></p>
<form>
  <input type=”text” name=”name” value=”John”/>
  <input type=”text” name=”password” value=”password”/>
  <input type=”text” name=”url” value=”http://ejohn.org/”/>
</form>

jQuery 代码:


$(“p”).append( $(“input”).map(function(){
  return $(this).val();
}).get().join(“, “) );

结果:


[ <p>John, password, http://ejohn.org/</p> ]
————————————————————————————————————–