开发者

Specify onmouseleave function that has 1 parameter

开发者 https://www.devze.com 2023-03-19 19:36 出处:网络
I have a dropdown list in my navbar. I do something a little different to usual drop down lists, instead of having the drop down list created & just hidden, I create(when a link is hovered over) &

I have a dropdown list in my navbar. I do something a little different to usual drop down lists, instead of having the drop down list created & just hidden, I create(when a link is hovered over) & destroy it(when you stop hovering over that link) dynamically using javascript.

My Problem: I am attempting to set onmouseleave on the dropdown list div(that I create in javascript). But the function that I want to be called on onmouseleave takes one parameter - how do I specify this parameter?

dropDownList.onm开发者_如何学编程ouseleave = onLeaveEx;  // how do I specify the 1st parameter? Such as onLeaveEx("aboutUsLink")  

    function onLeaveEx( linkName )
    {
        if ( navSubMenu != null )
        {
            navSubMenu.parentNode.removeChild( navSubMenu ); 
            navSubMenu = null;
        }
    }


Use an anonymous function:

dropDownList.onmouseleave = function() { onLeaveEx(my_parameter); };


What you want is a closure. This allows to pass some parameters beforehand:

function make_callback(link_name){
    function actual_event_handler(evt){
        //this inner function can use link_name
        //here
    }
    return actual_event_handler;
}

dropDownList.onmouseleave = make_callback("a linke name");
0

精彩评论

暂无评论...
验证码 换一张
取 消