测试代码:
<html> <head> <script type="text/javascript" src="/jquery/jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("#but").click(function(){ test(); }); }); function test(){ $("div").each(function(index){ var obj=$(this); var val=$("input",obj).val();//默认第一个匹配的元素 alert(index+":"+val); }); } </script> </head> <body> <h2>This is a heading</h2> <button type="button" id="but">Click me input</button> <div ><input type='text' value='1'/><input type='text' value='0'/></div> <div ><input type='text' value='2'/></div> <div ><input type='text' value='3'/></div> <div ><input type='text' value='4'/></div> <div ><input type='text' value='5'/></div> </body> </html>
jQuery(expression, [context]) 返回值:jQuery;
一般情况下不指定context参数,此时将在当前的 HTML document中查找 DOM 元素;如果指定了 context 参数,如上例:查找div中的input。