Visualizzazione dei risultati da 1 a 5 su 5
  1. #1

    is not a function... invece lo è!

    Ciao a tutti!
    Ho una funzione JavaScript:
    coloraCella : function() { alert("siii"); },
    richiamata da "initialize" in questo modo:
    this.coloraCella();

    ma firebug mi segnala: IS NOT A FUNCTION

    come mai? come posso rosolvere?

    codice:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    	<title>Tabella</title>
    	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    	<link rel="stylesheet" type="text/css" href="CSS/screen.css" />
    	<script type="text/javascript" src="./js/prototype.js"></script>
    
    	<script type="text/javascript">
    	
    	var EditTable = Class.create();
    
    	EditTable.prototype = {
    
    		t : null,
    		td : null,
    		tr : null,
    		nameClass : null,
    		parametersClass : null,
    
        	initialize: function () {			
        		
        			$$('table')[0].observe( 'click', function (event) { 
        					var elt = Event.element(event);
            				if( elt.nodeName == "TD" ) {
            					this.td = elt;
            					//alert("td = " +  this.td);
            					
            					this.tr = this.td.up('tr');
            					//alert("tr = " +  this.tr);
    							
            					this.t = this.td.up('table');
            					//alert("t = " +  this.t);
            	
            					
            					this.nameClass = this.t.down().textContent;
            					//alert("nameCLass = " +  this.nameClass);
    
    							//prelevo i parametri
            					var elt_thead = this.t.down('thead');
            					elt_thead = elt_thead.down('tr');
            					var par = new Array();  					
            					elt_thead.descendants().each(function(s, index) {
                					par[index] = s.textContent;
                					//alert("element : " + par[index]);
            					});
                				this.parametersClass = par;
                				//alert(this.parametersClass);
    
    							
                				this.coloraCella();
                				
            				} //if TD
        			}); //observe table
        			
    			}, //initialize
    		
    		coloraCella : function() {
    				alert("siiii");	
    			}
    	};
    
    	
    	var Table = function () {
    		var t = new EditTable;
    	}
    	
    	document.observe("dom:loaded", Table);
    	
    	</script>
    
    
    	
    </head>
    <body>
    
    
        <table>
        		<caption>
        		CAPTION
        		</caption>
                <tfoot>
                            <tr>
                                     <td>aaaaaa</td>
                            </tr>
                </tfoot>
                <thead>
                		<tr>
                			<th>
                			PARAMETRO_1
                			</th>
                			<th>
                			PARAMETRO_2
                			</th>
                		</tr>
                </thead>
                <tbody>
                            <tr>
                                    <td>primo</td>
                                    <td>uno</td>
                            </tr>
                             
                            <tr>
                                    <td>secondo</td>
                                    <td>due</td>
                            </tr>
                            
                            <tr>
                                    <td>terzo</td>
                                    <td>tre</td>
                            </tr>
                            <tr>
                                    <td>quarto</td>
                                    <td>quattro</td>
                            </tr>
                </tbody>
        </table>
        
    </body>
    </html>
    Grazie!

  2. #2
    nessuna risposta?
    cercherò di essere più chiaro e sintetio.
    Dando un occhiata al piccolo seguente script:
    codice:
    <script type="text/javascript">
    	
    	var EditTable = Class.create();
    
    	EditTable.prototype = {
    
        	initialize: function () {
    
    			$$('table')[0].observe( 'click', function (event) {			
                		                  this.stampa();
    				});
    		},
    		
    	stampa : function() {
    			alert("siiii");	
    		}
    	};
    
    	
    	var Table = function () {
    		var t = new EditTable;
    	}
    	
    	document.observe("dom:loaded", Table);
    	
    	</script>
    Quando clicco su una cella della tabella, dovrebbe apparirmi la scritta (alert) "siii"
    Invece la console di firebug mi segnala: this.stampa(); is not a function
    come mai? come posso risolvere?

  3. #3
    Moderatore di Annunci siti web, Offro lavoro/collaborazione, Cerco lavoro L'avatar di cavicchiandrea
    Registrato dal
    Aug 2001
    Messaggi
    26,133
    Prova a sostituire this con return oppure a togliere tutto lasciando solo stampa(), non vorrei che "ritenga" lo this di window e non dell'oggetto è solo un ipotesi non conosco bene prototype.



    P.S. Mi scuso con gli esperti se ho detto delle stupidagini
    Cavicchi Andrea
    Problemi con javascript, jquery, ajax clicca qui

  4. #4
    OK, se tolgo
    $$('table')[0].observe( 'click', function (event) {
    non mi da errori! però a me serve quell'istruzione!

    Quindi dovrei forse aggiungere la funzione bind() in questo modo:
    this.stampa.bind();
    giusto? o come andrebbe scritta?
    "Bind", se non dico fesserie, serve proprio per dirgli che l'elemento è "this" e non window...!?
    Grazie!

  5. #5
    Per ora ho sistemato così:

    codice:
        	initialize: function () {
    		         $$('table')[0].observe( 'click', this.stampa.bindAsEventListener(this));
    		},
    		
    	stampa : function(obj) {  
                             alert("siiii");	 
                    }
    Grazie Andrea!
    Aspetto ancora un pochino prima di mettere risolto!

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.