JQuery - only child

<html>
<head>
<title>jQuery only child example</title>
 
<script type="text/javascript" src="jquery-1.9.1.js"></script>
</head>
 
<body>
 
<h1>jQuery only child example</h1>
 
<ul>
	<li>ABC #1</li>
	<li>ABC #2</li>
	<li>ABC #3</li>
	<li>ABC #4</li>
	<li>ABC #5</li>
</ul>
 
<ul>
	<li>DEF #1</li>
</ul>
 
 <div>
	<p>Only div</p>
</div>

<ul>
	<li>GHI #1</li>
	<li>GHI #2</li>
</ul>
 
<button>li:only-child</button>
<button>:only-child</button>
<script type="text/javascript">
    $("button").click(function () {
      var str = $(this).text();
      $("li").css("background", "white");
      $(str).css("background", "coral");
    });
</script>
 
</body>
</html>
相關文章
相關標籤/搜索