2011-11-18 94 views
3

我正在使用.each函数来抓取div,但我无法将ID传递给我的模式框函数。这里是代码:传递ID属性

(function() { 
$('div.heriyah').each(function() { 
$(this).html('<div class="add"><div id="add_button_container"><div id="add_button" class="edit_links">+ ADD NEW CONTENT</div></div></div><div class="clear"></div><div class="placeable"></div></div>'); 

$('.add').click(function() { 
$.fallr('show', { 
      content  : '<iframe width="620" height="600" src="<? echo $URL ?>/manage_content.php?id=<? echo $pageID; ?>&div='+$(this).attr('id')+'"></iframe>', 
      width  : 620 + 5, // 100 = for width padding 
      height   : 600, 
      closeKey  : true, 
      closeOverlay : true, 
      buttons  : {} 
}); 
}); 

}); 

})();

我得到Undefined如果我请求我的模态框中的变量。

我用这个:

'+$(this).attr('id')+' 

抢我的模式对话框函数内部的ID。

感谢, 鼓手

回答

3

我不知道什么是fallr但试着改变你的代码如下:

(function() { 
    $('div.heriyah').each(function() { 
    $(this).html('<div class="add"><div id="add_button_container"><div id="add_button" class="edit_links">+ ADD NEW CONTENT</div></div></div><div class="clear"></div><div class="placeable"></div></div>'); 

    var curID = $(this).attr('id');//get the id before you go into fallr 

    $('.add').click(function() { 
    $.fallr('show', { 
       content  : '<iframe width="620" height="600" src="<? echo $URL ?>/manage_content.php?id=<? echo $pageID; ?>&div='+curID +'"></iframe>', 
       width  : 620 + 5, // 100 = for width padding 
       height   : 600, 
       closeKey  : true, 
       closeOverlay : true, 
       buttons  : {} 
    }); 
    }); 

    }); 
})(); 
+0

谢谢,这使得这么多的意义。我今天学了些新东西! – drummer392

0

这是因为`这是现在指的fallr

$('.add').click(function() { 
    var _this = $(this); 
    $.fallr('show', { 
       content  : '/manage_content.php?id=&div='+_this+'">', 
       width  : 620 + 5, // 100 = for width padding 
       height   : 600, 
       closeKey  : true, 
       closeOverlay : true, 
       buttons  : {} 
    }); 
});
2

您在SECOND匿名函数中引用$(this)。所以你要求.add $(this)不是div.heriyah这就是为什么你没有得到ID。

+0

谢谢,我今天学到了一些东西!大声笑=) – drummer392

0

在你的代码,你正在构建的iframe$(this)将是$('.add')的价值点,你原来div标签。

在绑定click事件之前,只需创建一个新变量并存储$(this).attr('id'),然后使用该值。

0

经典范围问题 - 您试图获取'.add'元素的id属性,因为您在该范围内。

缓存的$(本)在.each功能和使用:

$('div.heriyah').each(function() { 
$(this).html('<div class="add"><div id="add_button_container"><div id="add_button"  class="edit_links">+ ADD NEW CONTENT</div></div></div><div class="clear"></div><div class="placeable"></div></div>'); 

var that = $(this); 

$('.add').click(function() { 
    $.fallr('show', { 
      content  : '<iframe width="620" height="600" src="<? echo $URL ?>/manage_content.php?id=<? echo $pageID; ?>&div='+that.attr('id')+'"></iframe>', 
      width  : 620 + 5, // 100 = for width padding 
      height   : 600, 
      closeKey  : true, 
      closeOverlay : true, 
      buttons  : {} 
}); 
    }); 

});