/ / JavaScript: DRY mouseover/ mouseout event handlers - javascript, jquery, handler

JavaScript: DRY mouseover / mouseout obslužné rutiny udalostí - javascript, jquery, handler

Toto je môj kód:

    $rows
.on("mouseover", ".row", function () {
$(this).find(".label").show();
})
.on("mouseout", ".row", function () {
$(this).find(".label").hide();
});

Dá sa to SUŠIŤ?

odpovede:

4 pre odpoveď č. 1

Môžete viazať obe udalosti, počúvať event.name a potom použite jQuery.fn.toggle

$userRows.on("mouseover mouseout", ".row", function(event) {
$(this).find(".label").toggle( event.name == "mouseover" );
});

Som si celkom istý, že môžete tiež použiť jQuery.fn.hover:

$userRows.on("hover", ".row", function(event) {
$(this).find(".label").toggle( event.name == "mouseenter" );
});

alebo dokonca:

$userRows.on("hover", ".row", function(event) {
$(this).find(".label").toggle();
});

0 pre odpoveď č. 2

Čo takto:

$ rows.hover ( funkcia () { $ (this) .find (". label"). toggle (); }, funkcia () { $ (this) .find (". label"). toggle (); } );