English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

مетод jQuery hover()

أحداث jQuery

عندما يتم تمرير المؤشر فوق العنصر المحدد، تقوم دالة hover() بتحديد وظيفتين للتنفيذ.

تقوم هذه الطريقة بتشغيلmouseenterوmouseleaveالحوادث.

الطريقة المختصرة لاستدعاء دالة hover() هي: ;$(selector).mouseenter(function_in).mouseleave(function_out)

ملاحظة:عند تقديم وظيفة واحدة، تقوم دالة hover() بتنفيذ هذه الوظيفة لكل من حوادث mouseenter و mouseleave.

النحو:

$(selector).hover(function_in, function_out)

مثال

عندما يتم تمرير المؤشر فوق العناصر <p>، يتم تغيير لون الخلفية لكل عناصر <p>:

$(\"p\").hover(function(){
  $(this).css(\"background-color\", \"yellow\");
  }, function(){
  $(this).css(\"background-color\", \"lightblue\");
});
اختبار لمعرفة‹/›

إضافة تنسيق خاص لقائمة العناصر المطلوب عرضها عند التمرير فوقها:

$(document).ready(function(){
  $(\"li\").hover(function(){funcIn(this);}, function(){funcOut(this);});
});
function funcIn(x) {
  $(x).html(\"حادثة ضغط زر ENTER للفأرة يتم تفعيلها\");
  $(x).css(\"background\", \"yellow\");
}
function funcOut(x) {
  $(x).html(\"ت 触发 حادثة مغادرة الفأرة\");
  $(x).css("background", "white");
}
اختبار لمعرفة‹/›

إذا تم تحديد دالة واحدة فقط، فإنها ستتم تشغيلها في أحداث mouseenter و mouseleave بشكل متزامن:

$("div").hover(function(){
  $(this).css("background", randColor());
});
// دالة الحصول على لون عشوائي
function randColor() {
  return 'rgb('+Math.floor(Math.random()*256)+','+Math.floor(Math.random()*256)+',' 
  ','+Math.floor(Math.random()*256)+')';
}
اختبار لمعرفة‹/›

قيمة المتغير

المتغيروصف
function_inدالة تنفيذ عند دخول مؤشر الفأرة إلى العنصر
function_outدالة تنفيذ عند مغادرة مؤشر الفأرة عن العنصر

أحداث jQuery