0

我正在尝试显示一个带有与之关联的颜色框弹出窗口的超链接。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 源中的超链接,它可以正常工作并在 Colorbox 中正确显示。

任何帮助将不胜感激 :)

4

2 回答 2

2

您需要等到添加了链接,然后再调用colorbox()它的方法。

移动您的colorbox()方法,使其在您的innerHTML.

jQuery('a.popup').colorbox({ opacity:0.5 , rel:'group1' });
于 2013-02-13T18:59:02.617 回答
0

当您动态添加html时,添加的事件已经无法触发。试试下面的代码

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


function applycolorbox($elem) {
         $elem.colorbox({ opacity:0.5 , rel:'group1' });
}
于 2013-02-13T19:00:21.280 回答