联系我们
简单又实用的WordPress网站制作教学
当前位置:网站首页 > 网页前端技术 > 正文

jQuery函数的第二个参数获取指定上下文中的DOM元素 - Web前端

作者:98wpeu发布时间:2026-05-23分类:网页前端技术浏览:13


导读:JQuery函数的第二个参数可以指定DOM元素的搜索范围。第二个参数可分为以下类型domreferencejQuerywrAPPerdocument代码示例复制代码代码...
JQuery函数的第二个参数可以指定DOM元素的搜索范围。

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

domreference
jQuery wrAPPer
document

代码示例
复制代码 代码如下:
<!DOCtypehtml>
<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

标签:第二个文中函数元素参数Web


网页前端技术排行
最近发表
网站分类
标签列表