jQuery函数的第二个参数获取指定上下文中的DOM元素
来源:互联网 发布:客厅电脑主机 知乎 编辑:程序博客网 时间:2024/06/05 00:47
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
第二个参数可分为以下类型
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>
总结
第二个参数的不同的类型,对应的用法如下表所示。
$('input', $('form')).length
DOM reference$('input', document.forms[0]).length
document$('input', 'body').length
0 0
- jQuery函数的第二个参数获取指定上下文中的DOM元素
- jQuery -> 获取指定上下文中的DOM元素
- DOM获取指定的所有元素
- jquery 获取指定元素
- jQuery 获取指定元素
- 简谈jQuery选择器的第二个参数Context对象以及jQuery对象转换成DOM对象的方法
- 关于 jquery 的第二个参数 undefined
- 关于 jquery 的第二个参数 undefined
- jQuery()方法的第二个参数详解
- dom元素的获取
- DOM元素的获取
- jquery获取多个class的元素
- jquery 获取第一个元素的方法
- jQuery选择器的上下文参数
- 总结的一些jquery获取更改dom元素常用方法
- 总结的一些jquery获取更改dom元素常用方法
- jQuery -> 获取/设置/删除DOM元素的属性
- jQuery -> 获取/设置/删除DOM元素的属性
- 设置系统状态栏颜色
- javaweb学习总结(八)——HttpServletResponse对象(二)
- mysql二进制日志
- webmagic的设计机制及原理-如何开发一个Java爬虫
- Android开源代码汇总
- jQuery函数的第二个参数获取指定上下文中的DOM元素
- Eclipse快捷键和10个最有用的快捷键
- 134. Gas Station
- caffe源码阅读笔记(1):路线图
- 第四专题 Problem J
- 欢迎使用CSDN-markdown编辑器
- i2c驱动程序
- SpringBoot入门系列:第七篇 Spring Boot的测试
- phpmyadmin取消最大文件限制的更改解决方法