SORU
6 EKİM 2009, Salı


dinamik özelliği bir olaya bağlamak nasıl jQuery HTML öğesi eklendi

Dinamik olarak jQuery ile element ben bir ekleme özelliği bir olaya bağlamak istiyorum

Ama asla lidir işlevi çalışır. Eğer bu örnek çalışmıyor neden ve düzgün çalıştırmak için nasıl alabilirim işaret ederseniz memnun olurum:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"        
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="da" lang="da">
    <head>
      <title>test of click binding</title>

      <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js" type="text/javascript"></script>
      <script type="text/javascript">


    jQuery(function(){
      close_link = $('<a class="" href="#">Click here to see an alert</a>');
      close_link.bind("click", function(){
        alert('hello from binded function call');
        //do stuff here...
      });

      $('.add_to_this').append(close_link);
    });
      </script>
    </head>
    <body>
      <h1 >Test of click binding</h1>
      <p>problem: to bind a click event to an element I append via JQuery.</p>

      <div class="add_to_this">
        <p>The link is created, then added here below:</p>
      </div>

      <div class="add_to_this">
        <p>Another is added here below:</p>
      </div>


    </body>
    </html>

EDİT: bu yöntem için eklenen iki element içeren örnek düzenledim.Bu durumda, alert() aramayı hiçbir zaman yürütülür. (@bir yorum belirttiğin için Daff için teşekkürler)

CEVAP
1 EKİM 2012, PAZARTESİ


Tüm bu yöntemleri kabul edilmez. Sorununuzu çözmek için on yöntemi kullanmalısınız.

Eğer bir dinamik olarak eklenen öğe hedef istiyorsan kullanman gerekecek

$(document).on('click', selector-to-your-element , function() {
     //code here ....
});

bu .live() itiraz yöntemi değiştirin.

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • BachelorsPadTv

    BachelorsPad

    17 Ocak 2012
  • My name is Festis and I'm free

    My name is F

    2 EKİM 2011
  • Tips On Linux

    Tips On Linu

    26 Temmuz 2008