2012-02-08 71 views
0

所以,我有一個奇怪的錯誤。如果非常有用的話,jquery用預覽替換方塊中錯誤的img src。鼠標懸停代替錯誤img src

這是js代碼

$('.mini-info').bind('mouseenter', function() { 
    var xex = $(this).position().left; 
    var yey = $(this).position().top; 
    sim = $('img', this).attr('src'); 
    kuda = $('a', this).attr('href'); 


    $('#bcase div.'+$(this).attr('id')).addClass('active').css({ 
     'top' : yey, 
     'left' : xex 
    }).attr({'onclick': 'location.href="'+kuda+'"'}); 

    var omg = $('#bcase div.'+$(this).attr('id')+' div.b').length; 

    $('#bcase div.'+$(this).attr('id')+' div.b').css({ 
     'width': wid/omg 
    }); 
    }); 



    $('#bcase > div').bind('mouseleave', function() { 
    $(this).removeClass('active'); 
    $('.mini-info#'+$(this).attr('class')+' img').attr({'src': sim}); 
    }); 

這是可能的解決?

正常工作是當您將鼠標移動到預覽的一個方塊上時,腳本將在鼠標下改變圖像。它工作正常。但是上面的bug不好...

+0

,請複製粘貼相關代碼到你的問題,而不是讓我們去嘗試找到它在網頁中我們不熟悉用。 – jfriend00 2012-02-08 06:19:03

+0

我認爲這是生活中最好的例子。好的,只需一分鐘。 – Nixon 2012-02-08 06:28:32

+0

一個工作鏈接也值得包括,但是如果你只是粘貼相關代碼供人們看看,而不必反向工程你的頁面如何工作以及代碼在哪裏,你會得到更多的答案。在這種情況下,你已經包含了很多額外的東西。包括JUST相關部分的HTML和JS是最好的。 – jfriend00 2012-02-08 06:50:19

回答

1

在mouseleave事件中,您使用的sim變量在mouseenter事件中定義。
然後在快速鼠標打擊的情況下,其他div的mouseenter將在之前事件的mouseleave之前調用。

你必須改變你的拱門。例如,當您在mouseenter中創建新div時,此時綁定mouseleave事件。
東西艾克說(未測試):

$('.mini-info').bind('mouseenter', function() { 
    var xex = $(this).position().left; 
    var yey = $(this).position().top; 
    var sim = $('img', this).attr('src'); 
    kuda = $('a', this).attr('href'); 


    $('#bcase div.'+$(this).attr('id')).addClass('active').css({ 
    'top' : yey, 
    'left' : xex 
    }).attr({'onclick': 'location.href="'+kuda+'"'}) 
    .bind('mouseleave', function() { 
    $(this).removeClass('active'); 
    $('.mini-info#'+$(this).attr('class')+' img').attr({'src': sim}); 
    }); 

    var omg = $('#bcase div.'+$(this).attr('id')+' div.b').length; 

    $('#bcase div.'+$(this).attr('id')+' div.b').css({ 
    'width': wid/omg 
    }); 
}); 

看到的jsfiddle:http://jsfiddle.net/bouillard/ptJQy/

+0

非常感謝。我已測試過這些代碼,並且此修復程序無法解決問題。 – Nixon 2012-02-08 07:01:36

+0

也許在這個函數中加入'var sim'聲明sim。 – 2012-02-08 09:03:05

+1

就是這樣!添加一個'var' – 2012-02-08 09:17:23