<html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <head> <title>使用jQuery子元素过滤选择器title> <script language="javascript" type="text/javascript" src="jquery-2.1.4.js">script> <style type="text/css"> body { font-size: 12px; text-align: center } ul { width: 241px; list-style-type: none; padding: 0px } ul li { height: 23px; width: 60px; line-height: 23px; float: left; border-top: solid 1px #eee; border-bottom: solid 1px #eee; margin-bottom: 5px } .GetFocus { width: 58px; border: solid 1px #666; background-color: #eee } style> <script type="text/javascript"> // $(function(){ //增加每个父元素下的第2个子元素类别 // $("li:nth-child(2)").addClass("GetFocus"); // }) // $(function(){ //增加每个父元素下的第一个子元素类别 // $("li:first-child").addClass("GetFocus"); // }) // $(function(){ //增加每个父元素下的最后一个子元素类别 // $("li:last-child").addClass("GetFocus"); // }) $(function(){ //增加每个父元素下只有一个子元素类别 $("li:only-child").addClass("GetFocus"); }) script> head> <body> <ul> <li>Item 1-0li> <li>Item 1-1li> <li>Item 1-2li> <li>Item 1-3li> ul> <ul> <li>Item 2-0li> <li>Item 2-1li> <li>Item 2-2li> <li>Item 2-3li> ul> <ul> <li>Item 3-0li> ul> body> html>
[JQ权威指南]第四天:使用jQuery子元素过滤选择器
关注
打赏