Jquery通过指定层次关系获取元素
html代码
<body> <form id="form1" runat="server"> <span>dfdfdf</span> <label>子节点</label><input type="text" value="背景变成红色"/> <fieldset> <label>孙节点</label><input type="text" value="背景变成红色" /><span>dfdfdf</span> <label>成孙节点</label><input type="text" value="背景变成红色" /> <input type="text" value="背景变成红色" /><span>dfdfdf</span> </fieldset> </form> <label>form外</label><input type="text" value="背景变成红色" /> <fieldset> <label>form外</label><input type="text" value="背景变成红色" /> <input type="text" value="背景变成红色" /> </fieldset> <input type="text" value="背景变成红色" /> </body>
Jquery:
<script src="js/jquery-1.6.min.js" type="text/javascript"></script> <script type="text/javascript"> jQuery(function($){ // $("form input").css("backgroundColor","red");//获得form下面的所有input标签,祖先与父子的关系 // $("form>fieldset>input").css("color","red"); //获得form下面fieldset下面的所有input标签,父子关系 // $("form>fieldset>input").css("backgroundColor","red"); // $("label+input").css("backgroundColor","red");//获得所有label后面的input标签,label和input必须在一起,前后关系 $("label~input~span").css("backgroundColor","red");//获得label后面的input后面的span标签 }) </script>
孜孜不倦,学习不止