传递ID属性

时间:2011-11-18 16:43:23

标签: jquery scope

我正在使用.each函数抓取div,但是我无法将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。

谢谢, 鼓手

5 个答案:

答案 0 :(得分: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     : {}
    }); 
    }); 

    }); 
})();

答案 1 :(得分:2)

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

答案 2 :(得分:0)

这是因为`这现在指的是秋季ep>

$('.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     : {}
    }); 
}); 

答案 3 :(得分:0)

在您构建iframe的代码中,$(this)的值为$('.add')您的原始div } tag。

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

答案 4 :(得分:0)

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

.each函数中缓存$(this)并使用:

$('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     : {}
}); 
    }); 

});