التابع ‎.first()‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎ في jQuery

من موسوعة حسوب
اذهب إلى التنقل اذهب إلى البحث
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

‎.first()‎

القيمة المعادة

يُعيد كائنًا من النوع jQuery.

الوصف

يُقلِّص هذا التابع مجموعة العناصر المطابقة إلى الأول في المجموعة.

‎.first()‎

أُضيف مع الإصدار: 1.4.

لا يقبل هذا التابع أي وسائط.

إذا أُدخل كائن jQuery يمثل مجموعة من عناصر DOM، يقوم التابع ‎.first()‎ ببناء كائن jQuery جديد من العنصر الأول في تلك المجموعة.

إليك صفحة بها قائمة بسيطة:

<ul>
  <li>list item 1</li>
  <li>list item 2</li>
  <li>list item 3</li>
  <li>list item 4</li>
  <li>list item 5</li>
</ul>

يمكننا تطبيق هذا التابع على مجموعة عناصر القائمة:

$( "li" ).first().css( "background-color", "red" );

نتيجة هذا الاستدعاء تعطي خلفية حمراء للعنصر الأول.

أمثلة

تمييز أول عنصر <span> في الفقرة <p> بضبط لون خلفيته background-color إلى الأصفر:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>first demo</title>
  <style>
  .highlight{
    background-color: yellow
  }
  </style>
  <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
 
<p>
  <span>Look:</span>
  <span>This is some text in a paragraph.</span>
  <span>This is a note about it.</span>
</p>
 
<script>
$( "p span" ).first().addClass( "highlight" );
</script>
 
</body>
</html>

مصادر