2013-02-13 95 views
0

我試圖顯示一個超鏈接,它有一個與之相關的colorbox彈出窗口。 的JavaScript是:使用Colorbox和JavaScript函數

function bid() {  
    var bid = document.getElementById("bid").value; 
    if (bid>0 && bid<=100) { 
    var per = 3.50; 
    } else if (bid>100 && bid<=200) { 
    var per = 3.40; 
    } else if (bid>200 && bid<=300) { 
    var per = 3.30; 
    } 

var fee = Math.round(((bid/100)*per)*100)/100; 
var credit = 294.9; 

    if (fee>credit) { 
    var message = 'Error'; 
    } else { 
    var message = '<a class="popup" href="URL">The link</a>'; 
    } 

    document.getElementById("bidText").innerHTML=message; 
} 

JavaScript的正常工作,並顯示在適當的條件下的環節,但問題是,點擊該鏈接時,沒有應用的彩盒和加載頁面的超鏈接正常。

我在頭下面的代碼:

jQuery(document).ready(function() { 
    jQuery('a.popup').colorbox({ opacity:0.5 , rel:'group1' }); 
}); 

如果我只輸出標準的HTML源的超級鏈接,它工作正常,並正確顯示在彩盒。

任何幫助,將不勝感激:)

+0

我不熟悉的顏色框,但嘗試改變它說:HREF =「鏈接」到HREF =「#」 – Doug 2013-02-13 18:57:34

+0

只是爲了確認,你加載jQuery庫? – Toping 2013-02-13 18:59:01

回答

2

你需要等待,直到你附加的鏈接調用就可以了colorbox()方法之前。

移動您的colorbox()方法,使其在您的innerHTML之後。

jQuery('a.popup').colorbox({ opacity:0.5 , rel:'group1' }); 
+0

雖然這就是爲什麼他使用document.ready – Toping 2013-02-13 18:59:48

+1

document.ready在這裏沒有意義。當ready事件觸發時,'.popup'項目可能會或可能不會在DOM中。 Zak的回答是正確的,海報需要等待,直到他在查詢DOM之前將錨元素添加到DOM。 – Jack 2013-02-14 07:46:53

0

當您動態添加html時,添加的事件已經無法觸發。 試試下面的代碼

jQuery(document).ready(function() { 
     $("a.popup").on("click", function(event){ 
     applycolorbox($(this)); 
}); 


function applycolorbox($elem) { 
     $elem.colorbox({ opacity:0.5 , rel:'group1' }); 
}