English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Quando il puntatore del mouse si trova sopra l'elemento selezionato, il metodo hover() specifica due funzioni da eseguire.
Questo metodo attiva contemporaneamentemouseenteremouseleaveeventi.
La chiamata al metodo hover() è abbreviata come: $(selector).mouseenter(function_in).mouseleave(function_out)
Attenzione:Quando si passa un singolo funzione, il metodo hover() esegue la funzione per entrambi gli eventi mouseenter e mouseleave.
$(selector).hover(function_in, function_out)
Modificare il colore di sfondo di tutti gli elementi <p> quando il puntatore del mouse è sopra di essi:
$("p").hover(function(){ $(this).css("background-color", "giallo"); }, function(){ $(this).css("background-color", "azzurro chiaro"); });测试看看‹/›
Aggiungere uno stile speciale per elencare gli elementi da hoverare:
$(document).ready(function(){ $("li").hover(function(){funcIn(this);}, function(){funcOut(this);}); }); function funcIn(x) { $(x).html("l'evento di pressione <b>ENTER</b> del mouse è stato attivato"); $(x).css("background", "giallo"); {} function funcOut(x) { $(x).html("attivare l'evento di uscita del mouse"); $(x).css("background","white"); {}测试看看‹/›
如果仅指定一个函数,则将同时为mouseenter和mouseleave事件运行该函数:
$("div").hover(function(){ $(this).css("background",randColor()); }); //获取随机颜色函数 functionrandColor(){ return'rgb('+Math.floor(Math.random()*256)+','+Math.floor(Math.random()*256)+', ','+Math.floor(Math.random()*256)+'); {}测试看看‹/›
参数 | 描述 |
---|---|
function_in | 当鼠标指针进入元素时执行的功能 |
function_out | (可选)当鼠标指针离开元素时执行的函数 |