开发者

Give this function an id

We are making a advert div, and we have this function 开发者_C百科on mouse over and mouse out an effect takes place. The issue is I cannot run more than one of these functions on same page, I suppose because it does not possess an ID.

Code is

Function mouseOver ()
{
    document.getelementbyid("flashdiv").classname="flash-abg";
}
Function mouseOut ()
{
    document.getelementbyid("flashdiv").classname="flash-I";
}

So how would I. Run two of these on one page, I am on iPad so difficult to set styling on here, etc. So apologies.

Basically the above code accompanies HTML and CSS to display hover effect on a advert div, we have all divs working correctly individually but because we have no I'd for the functions, they don't work together correctly when we have more than one on a page. As you can see I am crap at js.


If you want the same function to be binded to different elements, if correclty binded this is a reference to the source of the event (the div to which the event is attached). That should allow you to bind the same function to different elements. In your code just replace document.getelementbyid("flashdiv") for this.


Assuming these are event handlers, we are missing the code registering them. You could write a function taking the id which returns an event handler for one div in the following way:

function createMouseOver(id) {
    return function () {
        document.getelementbyid(id).classname="flash-abg";
    }
}

Then register the result of createMouseOver('someid'), which is a function with the correct id in its closure.

Alternatively, don't use ids at all and find all your ad divs by class -- and register event handlers for all elements found by class.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜