First (:first)
Description and Syntax
$(':first')
select the first element within the matched set.
$('li:first')
selects the first<li>
element$('.myclass:first')
selects the first element with the classmyclass
Examples
The :first
pseudo-class is shorthand for
:eq(0)
. It could also be written as :lt(1)
.
The following code selects the first paragraph.
<html>
<head>
<script src="http://java2s.com/style/jquery-1.8.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){<!--from w w w.ja va 2 s . co m-->
var str = $("p:first").text()+"added";
$("p:last").html(str);
});
</script>
</head>
<body>
<body>
<p>java2s.com</p>
<p>java2s.com</p>
<p>java2s.com</p>
<p>java2s.com</p>
</body>
</html>
The code above generates the following result.
Select the first span element
The following code selects the first span
.
<html>
<head>
<style>
.test{ border: 1px solid red; }
</style>
<!--from w w w . j a va 2s .c o m-->
<script src="http://java2s.com/style/jquery-1.8.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("span:first").text($(":hidden", document.body).length + " hidden elements.");
});
</script>
</head>
<body>
<span></span>
<div></div>
<div style="display:none;">java 2s.c om!</div>
<div></div>
<div></div>
<form>
<input type="hidden" />
<input type="hidden" />
<input type="hidden" />
</form>
<span></span>
</body>
</html>
The code above generates the following result.
Combine first with id selector
The following code combine the first selector and id selector.
<html>
<head>
<script src="http://java2s.com/style/jquery-1.8.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){<!-- ww w . j a v a2 s . c om-->
$("#container").click(function (e) {
var $ch = $(e.target).children();
$("#results span:first").text($ch.length);
e.preventDefault();
return false;
});
});
</script>
</head>
<body>
<body>
<div id="container">
<div>
<p>This <span>is the <em>way</em> we</span>
write <em>the</em> demo,</p>
</div>
</div>
<span id="results">Found <span>0</span> children in <span>TAG</span>.</span>
</body>
</html>
The code above generates the following result.
Select first row
The following code selects the first row.
<html>
<head>
<script src="http://java2s.com/style/jquery-1.8.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){<!--from ww w . ja v a 2 s . c om-->
$("tr:first").css("font-style", "italic");
});
</script>
</head>
<body>
<table>
<tr><td>Row 1</td></tr>
<tr><td>Row 2</td></tr>
<tr><td>Row 3</td></tr>
</table>
</body>
</html>
The code above generates the following result.
...
Attribute not equal ([foo!=bar])
Attribute begins with ([foo^=bar])
Attribute ends with ([foo$=bar])
Attribute contains ([foo*=bar])
Attribute contains word ([foo~=bar])
Attribute contains prefix ([foo|=bar])
Attribute exists $("[attributeName*='value'...
Form selector
Form input selector (:input)
Form text fields (input:text)
Form Password field (input:password)
Form Radio button (input:radio)
Form Checkbox (input:checkbox)
Form Submit button (input:submit)
Form Image button (input:image)
Form Reset button (input:reset)
Form button (input:button)
Form File upload (input:file)
Form Enabled form element (input:enabled)
Form Disabled form element (input:disabled)
Form Checked box (input:checked)
Form Selected option (input:selected)
Element at index (:eq(n))
Greater than (:gt(n))
Less than (:lt(n))
Even element (:even)
Odd element (:odd)
parent (:parent)
Contains text (:contains(text))
Contains element (:has(E))
Visible (:visible)
Hidden (:hidden)
Header element (:header)
Currently animating (:animated)
$(this) selector
Custom User Selectors
...
Attribute not equal ([foo!=bar])
Attribute begins with ([foo^=bar])
Attribute ends with ([foo$=bar])
Attribute contains ([foo*=bar])
Attribute contains word ([foo~=bar])
Attribute contains prefix ([foo|=bar])
Attribute exists $("[attributeName*='value'...
Form selector
Form input selector (:input)
Form text fields (input:text)
Form Password field (input:password)
Form Radio button (input:radio)
Form Checkbox (input:checkbox)
Form Submit button (input:submit)
Form Image button (input:image)
Form Reset button (input:reset)
Form button (input:button)
Form File upload (input:file)
Form Enabled form element (input:enabled)
Form Disabled form element (input:disabled)
Form Checked box (input:checked)
Form Selected option (input:selected)
Element at index (:eq(n))
Greater than (:gt(n))
Less than (:lt(n))
First (:first)
Last (:last)Even element (:even)
Odd element (:odd)
parent (:parent)
Contains text (:contains(text))
Contains element (:has(E))
Visible (:visible)
Hidden (:hidden)
Header element (:header)
Currently animating (:animated)
$(this) selector
Custom User Selectors
...