添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接


jQuery函数的第二个参数可以指定DOM元素的搜索范围。

第二个参数可分为以下类型

  • DOM reference
  • jQuery wrapper
  • document

代码示例

<!DOCTYPE html>
<html>
<body>
<form>
<input name="" type="checkbox"/>
<input name="" type="radio"/>
<input name="" type="text"/>
<input name="" type="button"/>
</form>
<form>
<input name="" type="checkbox"/>
<input name="" type="radio"/>
<input name="" type="text"/>
<input name="" type="button"/>
</form>
<input name="" type="checkbox"/>
<input name="" type="radio"/>
<input name="" type="text"/>
<input name="" type="button"/>

<script type="text/javascript" src="jquery-1.11.1.js"></script>
<script type="text/javascript">
// search within all form elements, using a wrapper for context,
// alerts "8 inputs"
alert("selected" + $('input', $('form')).length + ' inputs');

// search with the first form element, using DOM reference as the context,
// alerts "4 inputs"
alert("selected" + $('input', document.forms[0]).length + ' inputs');

// search within the body element for all input elements using an expression,
// alerts "12 inputs"
alert("selected" + $('input', 'body').length + ' inputs');
</script>
</body>
</html>


总结

第二个参数的不同的类型,对应的用法如下表所示。

类型

用法

jQuery wrapper

​$('input', $('form')).length​

DOM reference

​$('input', document.forms[0]).length​

document

​$('input', 'body').length​

参考

  • jQuery Cookbook -> 1.4 Selecting DOM Elements Within a Specified Context.
【Kotlin 协程】协程上下文 ( 协程上下文构成要素 | 指定协程上下文元素组合 | 协程上下文元素的继承关系 | 协程上下文元素的几种指定形式 | 默认 | 继承 | 自定义指定 )

一、协程上下文构成要素、二、指定协程上下文元素组合、三、协程上下文元素的继承关系、四、协程上下文元素的几种指定形式 ( 默认 | 继承 | 自定义指定 )