.first()
Syntax
.first()
Parameters
None
Return value
The new jQuery object.
Description
Reduce the set of matched elements to the first in the set.
Examples
Consider a page with a simple list as follows:
<ul>
<li>list item 1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ul>
$('li').first().css('background-color', 'red'); marks item 1 red.
The following code get first and last
<!DOCTYPE html>
<html>
<head>
<script src="http://java2s.com/Book/JavaScriptDemo/jQuery/jquery-1.8.0.min.js">
</script>
<script>
$(function(){
var listElements = $("li");
var firstEl = listElements.first();
var lastEl = listElements.last();
document.writeln("firstEl value:" + firstEl.html());
document.writeln("firstEl length:" + firstEl.length);
document.writeln("lastEl value:" + lastEl.html());
document.writeln("firstEl length:" + firstEl.length);
});
</script>
</head>
<body>
<ul>
<li>A</li>
<li>B</li>
<li>C</li>
</ul>
</body>
</html>
Home
JavaScript Book
jQuery
JavaScript Book
jQuery
DOM:
- jQuery DOM
- $("html tags"):generate code with the jQuery wrapper function.
- .add()
- .addClass()
- .after()
- .andSelf()
- .append()
- .appendTo()
- .attr()
- .before()
- .children()
- .clone()
- .closest()
- .contents()
- .css()
- .detach()
- .filter()
- .first()
- .get()
- .has()
- .hasClass()
- .height()
- .html()
- .index()
- .innerHeight()
- .innerWidth()
- .insertAfter()
- .insertBefore()
- .is()
- .last()
- .map()
- .next()
- .nextAll()
- .nextUntil()
- .not()
- .offset()
- .offsetParent()
- .outerHeight()
- .outerWidth()
- .parent()
- .parents()
- .parentsUntil()
- .position()
- .prepend()
- .prependTo()
- .prev()
- .prevAll()
- .prevUntil()
- .remove()
- .removeClass()
- .removeAttr()
- .replaceAll()
- .replaceWith()
- .siblings()
- .scrollLeft()
- .scrollTop()
- .slice()
- .text()
- .toArray()
- .toggleClass()
- .unwrap()
- .val()
- .wrap()
- .wrapAll()
- .wrapInner()
- .width()