1 回答

TA貢獻1836條經(jīng)驗 獲得超4個贊
jQuery函數(shù)的第二個參數(shù)可以指定DOM元素的搜索范圍。
第二個參數(shù)可分為以下類型
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>
總結(jié)
第二個參數(shù)的不同的類型,對應的用法如下表所示。 類型 用法 jQuery wrapper $('input', $('form')).length DOM reference $('input', document.forms[0]).length document $('input', 'body').length
- 1 回答
- 0 關(guān)注
- 587 瀏覽
添加回答
舉報