ต้นฉบับ
$(document).ready(function() {
$("ul#topnav li").hover(function() { //Hover over event on list item
$(this).css({ 'background' : '#1376c9 url(topnav_active.gif) repeat-x'}); //Add background color + image on hovered list item
$(this).find("span").show(); //Show the subnav
} , function() { //on hover out...
$(this).css({ 'background' : 'none'}); //Ditch the background
$(this).find("span").hide(); //Hide the subnav
});
});
$(document).ready(function() {
// แก้ code ให้โชว์ตลอด
$("ul#topnav li").mouseenter(function() { //Hover over event on list item
var li = $(this);
li.parent().children("li").find("span").hide();
var span = li.find("span");
span.css({ 'background' : '#1376c9 url(topnav_active.gif) repeat-x'}); //Add background color + image on hovered list item
span.show(); //Show the subnav
};
// สั่งให้ตัวแรก โชว์
$("ul#topnav li:first").mouseenter();
// หรือ
//$("ul#topnav li#preshow").mouseenter();
});
ใช้ mouse enter ดีกว่า hover เพราะ hover มันจะ trigger ถี่ๆ ในขณะที่ mouse ลอยทับอยู่
ส่วน mouseenter จะ trigger ครั้งเดียวคือ เมื่อ ทouse เข้า