2012-02-08 25 views
0

我有一個Galleriffic,一個流行的jQuery插入顯示與他們附近的HTML圖像的問題。Galleriffic:它加載之前我的jquery代碼,其中的對象內部加載

在該HTML,我有一個圖像,用作按鈕:​​

<head></head>標籤之間,我有這樣的jQuery代碼:

<script type="text/javascript"> 
$(document).ready(function() { 
    $(".my_button").click(function(){ 
    $.get("page.php",function(data) 
    { 
     $('#mydiv').html(data); 
    }); 
    }); 
}); 
</script> 

的問題是,jQuery代碼加載之前 Galleriffic,所以當加載$(".my_button").click(){}事件時沒有按鈕。

有沒有辦法強制Galleriffic加載之前的jQuery代碼?

下面是一些代碼儘可能更加清晰:

<script type="text/javascript"> 
$(document).ready(function() { 

$(".my_button").click(function(){ 

$.get("pagea.php",function(data) 
{ 

$('#mydiv').html(data); 
}); 
}); 
}); 
</script> 

///some html 

<script type="text/javascript" src="Galleria Borse/js/jquery.history.js"></script> 
// Galleriffic libraries calls 
<!-- We only want the thunbnails to display when javascript is disabled --> 
<script type="text/javascript"> 
document.write('<style>.noscript { display: none; }</style>'); 
</script> 

<div class="navigation-container"> 
<div id="thumbs" class="navigation"> 
<a class="pageLink prev" style="visibility: hidden;" href="#" title="Previous Page"></a> 
<ul class="thumbs noscript"> 
//and other galleriffic containers and divs 
<img class="my_button" src="img/button.gif" /> //this is the button 

<script type="text/javascript"> 
jQuery(document).ready(function($) { //some Galleriffic code (Jquery code) } 

我試圖把代碼$(".my_button").click(function(){});無處不在:身體內部,在頁面的底部,甚至進入Galleriffic庫。

回答

0

你有沒有嘗試這個辦法:晚

jQuery(document).ready(function($) { 
    //some Galleriffic code (Jquery code) 
    // Initialize Minimal Galleriffic Gallery 
    $('#thumbs').galleriffic({ 
    imageContainerSel:  '#slideshow', 
    controlsContainerSel: '#controls' 
    }); 
    $(".my_button").click(function(){ 
    $.get("pagea.php",function(data){ 
     $('#mydiv').html(data); 
    }); 
    }); 
}); 
+0

我試過了,但在此之前的按鈕出現的「prev_photo」和「next_photo」鏈接,但Galleriffic後剩下的這段時間負載是我的代碼後加載jQuery代碼仍然加載。 Howefer我有//初始化高級Galleriffic圖庫_代替://初始化最小Galleriffic圖庫_和比你的例子更加碼...如果它是有用的...... – 2012-02-08 13:08:54

+0

我已經找到了解決辦法,它不是那麼優雅但它的工作原理。 首先,我把我的jquery代碼放在$(window).load(function() 而不是(document).ready(function($),所以它會在整個頁面加載後運行。 但是當我爲了解決這個問題,我還把Jquery代碼放在Galleriffic函數中,名爲 「function pageload(hash)」,之前就把它放在了Galleriffic的內容中,因爲這個按鈕已經被加載了「返回」聲明,以便在任何時候更改Galleriffic的內容時刷新。 – 2012-02-08 15:08:42

+0

花了整整一個上午和下午的一個下午後,我只希望這對別人有用;) – 2012-02-08 15:08:55

0

有一點,但無論如何:)。有一個解決方案,做到這一點,你甚至可以把你的代碼放到

$(document).ready();

的問題是,正如你說的,當你要綁定單擊事件,沒有什麼綁定它來。那麼,爲什麼不把它綁定到不同的東西呢。存在與

。對()

因爲jQuery的1.7(左右)一posibility。在這個版本中.on也具有.bind,.delegate和.live的功能。 我們對.delegate種類的風格感興趣:)。

所以不是

$(".my_button").click(function(){}) 

使用

$('#foo').on('click','.my_button', function(){}) 

是什麼做的是,你是結合.on event#foo,可以是一切事情,那就是在頁面上從一開始(可以說$(document))。並說「觸發點擊事件,當你看到一些.my_button點擊。這意味着,當.my_button終於來臨,它會工作:)。

順便說一句,這是解決與Galleriffic另一個問題的方式‘harcore’定製:)這是將動作附加到主幻燈片圖像(或其包裝)的唯一方式,因爲galleriffic仍在刪除和附加這些結構。