jQuery筛选

时间:2023-03-09 22:46:28
jQuery筛选

1.filter筛选出与指定表达式匹配的元素集合

  html:

<p>Hello</p><p>Hello Again</p><p class="selected">And Again</p>

    js:

$("p").filter(".selected")

   结果:

<p class="selected">And Again</p>

2.map将一组元素转换成其他数组

  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>

  js:

$("p").append( $("input").map(function(){
return $(this).val();
}).get().join(", ") );//将所有input表单中的value值插入到p标签中

  结果:

<p>John, password, http://ejohn.org/</p> 

3.contents()查找匹配元素内部所有的子节点(包括文本节点)。如果元素是一个iframe,则查找文档内容

  html:

<p>Hello <a href="http://ejohn.org/">John</a>, how are you doing?</p>

    JS:

$("p").contents().not("[nodeType=1]").wrap("<b/>");//给P标签中,节点不为1的内容都加上p标签

    结果:

<p><b>Hello</b> <a href="http://ejohn.org/">John</a>, <b>how are you doing?</b></p>