簡體   English   中英

Appgyver-單擊JQuery不適用於動態編寫的項目

[英]Appgyver - JQuery on click not working for dynamically written items

我正在使用appgyver(javascript)編寫單頁應用程序。 有許多列表是使用javascript動態編寫的(遵循外部API調用)。 每個列表包含一個項目,當單擊/按下該項目時,我想觸發一個不同的js方法。

  function write_a_list(some_array)
    {
        $('#some_list').html("");

        for ( indexor =0; indexor < some_array.length; indexor++)
          {
              var this_option_div = $('<div>');
              this_option_div.addClass('item');
              this_option_div.addClass('some_list_item');
              this_option_div.html("Button: " + indexor);
              $('#some_list').append(this_option_div);
          }
    }

  $(document).on('click', '.some_list_item', function()
  {
    supersonic.logger.debug('some_list_item clicked');
    alert('some_list_item clicked');
  });

例如,將數組傳遞給write_a_list函數。 如果單擊了某個項目,則應將其檢測到並發出警報。 但是,沒有觀察到此行為。 有沒有一種方法可以使用jquery的“ on”方法來實現?

您正在使用this_option_div.addClass('some_list_item'); 然后使用可能不存在的ID調用click事件。

更改為:

$(document).on('click', '.some_list_item', function() { ... }

編輯////

您可以嘗試使用非動態的最接近的父選擇器代替文檔嗎?

$('.static-wrapper').on('click', '.some_list_item', function() { ... }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM