Is JavaScript single threaded? If not, how do I get synchronized access to shared data?

前端 未结 7 1922
执笔经年
执笔经年 2020-12-17 01:12

I have a web page with DIVs with a mouseover handler that is intended to show a pop-up information bubble. I don\'t want more than one info bubble

7条回答
  •  一向
    一向 (楼主)
    2020-12-17 01:39

    Similar to what pkaeding said, it's hard to guess the problem without seeing your markup and script; however, I'd venture to say that you're not properly stopping the event propagation and/or you're not properly hiding the existing element. I don't know if you're using a framework or not, but here's a possible solution using Prototype:

    // maintain a reference to the active div bubble
    this.oActiveDivBubble = null;
    
    // event handler for the first div
    $('exampleDiv1').observe('mouseover', function(evt) {
        evt.stop();
        if(this.oActiveDivBubble ) {
            this.oActiveDivBubble .hide();
        }
        this.oActiveDivBubble = $('exampleDiv1Bubble');
        this.oActiveDivBubble .show();
    
    }.bind(this));
    
    // event handler for the second div
    $('exampleDiv2').observe('mouseover'), function(evt) {
        evt.stop();
        if(this.oActiveDivBubble) {
            this.oActiveDivBubble.hide();
        }
        this.oActiveDivBubble = $('exampleDiv2Bubble');
        this.oActiveDivBubble .show();
    }.bind(this));
    

    Of course, this could be generalized further by getting all of the elements with, say, the same class, iterating through them, and applying the same event handling function to each of them.

    Either way, hopefully this helps.

提交回复
热议问题