创建多个功能

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

标签: javascript jquery

我将尽力使这个问题变得清晰。

这是我正在尝试做的事情:

元素: - 按钮 - 模态框 - 功能

申请要求:

  1. 我需要在页面上有一个按钮,当点击它时会调用一个函数 叫newcontent。该功能需要拥有它自己的功能 唯一ID,因为我在模式框中显示了特定的pageID
  2. 在模态框功能中,我调用特定的特定页面 ID,但我也使用.each函数来获取特定的DIV ID 从中按下按钮。
  3. 这是当前的模态框代码:

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

    我会收到这样的错误:

    uncaught  exception: Can't create new message with content: "<iframe 
    width=620" height="600" 
    src="http://www.brandonrray.com/Heriyah/admin/manage_content.php?id=1&div=sermons_home"></iframe>",
    past message with content "<iframe width=620" height="600" 
    src="http://www.brandonrray.com/Heriyah/admin/manage_content.php?id=1&div=up_events_home"></iframe>"
    is still active
    

    但我知道这是因为我试图调用相同的函数10次,具体取决于页面上有多少div。

    按钮Jquery接线:

    $('div.heriyah').each(function() { 
    $('div.heriyah').append('<div id="add_button_container"><a onClick=newcontent_'+ this.id +'();return false><div id="add_button" class="edit_links">+ ADD NEW CONTENT</div></a></div></div><div class="clear"></div><div class="placeable"></div>');
    }); 
    

    任何人都可以推动我为这个应用程序正确的方向,如果你需要我更清楚,请告诉我。我不想再次被这些论坛踢掉!

1 个答案:

答案 0 :(得分:3)

不是每次单击按钮时都应用each,而是使用回调函数并使用存储在按钮元素中的参数触发newContent。该参数也可以是按钮的任何属性。

$('button[class=yours]').click(function () {newContent(this.id)});
// If you want to pass the DIV ID from the button
// $('button[class=yours]').click(function () {newContent($(this).attr('div-id')});
//
var newContent = function (uniqueId) {
    $.fallr('show', {
              content     :  '<iframe width=620" height="600" src="<? echo $URL ?>/manage_content.php?id=<? echo $pageID; ?>&div='+ uniqueId +'"></iframe>',
              width       : 620 + 5, // 100 = for width padding
              height         : 600,
              closeKey        : true,
              closeOverlay    : true,
              buttons     : {}
          }); 
};