IProgrammatori.it

Forum programmazione

Forum di supporto alla programmazione in generale e nei principali linguaggi.

It is currently Fri Mar 12, 2010 7:56 am

All times are UTC + 2 hours




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: [Jquery] Eventi relativi ad elementi generati
PostPosted: Sat Jun 20, 2009 4:24 pm 
Newbies
Joined: Mon Nov 13, 2006 4:12 pm
Posts: 4

Ciao a tutti..
ho un problemino che da qualche giorno mi tormenta.
In pratica non riesco a applicare eventi (tipo: click, hover ecc) ad elementi generati da javascript. Mi spiego: diciamo che io tramite jquery genero in un tag preciso, ad esempio $("div#container"), un tasto input button utilizzando o il metodo append() o html().

codice:
$("div#container").html("<input type=\"button\ class=\"miobottone\""");


Il tasto viene generato correttamente. Successivamente se io volessi però applicare un'altro evento relativo al tasto generato ad esempio un click:

codice:
$("input.miobottone").click(function(){ alert("ciao mondo!!"); });


questo evento non parte mai.. e lo stesso vale per tutti gli elementi generati col DOM.
Non riesco a spiegarmi il motivo. Mi piacerebbe saperlo anche se è una cosa sostanzialmente impossibile. Qualcuno a qualche idea?

Fatemi sapere..Grazie mille

P.s. Il codice che ho postato è solo dimostrativo e forse con qualche piccolo errore.


Offline
 Profile  
 Post subject: Re: [Jquery] Eventi relativi ad elementi generati
PostPosted: Sat Jun 20, 2009 5:49 pm 
Admin
User avatar
Joined: Tue Jun 06, 2006 4:00 pm
Posts: 2294
Location: Italia

Mah.. invece funziona..
avrai fatto qualche errore di sintassi.. lo usi firebug? Usalo se programmi in javascript..
Code:
<html>
   <head>
   </head>
      <body>
      <script type="text/javascript" src="http://www.google.com/jsapi"></script>
      <script type="text/javascript">
          // You may specify partial version numbers, such as "1" or "1.3",
          //  with the same result. Doing so will automatically load the
          //  latest version matching that partial revision pattern
          //  (i.e. both 1 and 1.3 would load 1.3.2 today).
          google.load("jquery", "1.3.2");
          google.setOnLoadCallback(function() {
               // Place init code here instead of $(document).ready()
                $("body").html('<input type="button" class="miobottone" value="Test">');
             $("input.miobottone").click(function(){ alert("ciao mondo!!"); });
           });
      </script>
   </body>
</html>

_________________
Big-Bug cerca collaboratori.
Cerca di scrivere con un italiano corretto.


Offline
 Profile E-mail  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC + 2 hours


 Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
 
cron