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

    A pro di addEventListener .......

    Ciao.
    Voglio passare ad una gestione + avanzata
    degli eventi ma sono incastrato su un semplice script (2 righe )
    Prima facevo una cosa del genere:
    Codice PHP:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd"
    >
    <
    html>
    <
    head>
    <
    title>Untitled Document</title>
    <
    meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <
    script type="text/javascript" language="javascript">
    openLink=function()
    {
            var 
    div=document.getElementById('testdiv');

            
    div.onclick=function(){

                  
    alert('Reacting to Event bubble phase');

             }
    }
    window.onload=openLink;
        
    </script>
    </head>

    <body>
        <div id="testdiv">

        

    [url="#"]Click here activate the alert method[/url]</p>

        </div>
    </body>
    </html> 
    Adesso almeno su Firefox dovrei fare una cosa del
    genere :
    Codice PHP:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd"
    >
    <
    html>
    <
    head>
    <
    title>Untitled Document</title>
    <
    meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <
    script language="javascript">
    alertBox=function()
        {
            
    alert('Reacting to Event bubble phase');
        }
        var 
    div=document.getElementById('testdiv');
        
    div.addEventListener('click',alertBox,false);
    </script>
    </head>
    <body>
        <div id="testdiv">
            [url="#"]Click here activate the alert method[/url]
        </div>
    </body>
    </html> 
    Ma non funziona (( mi sapete spiegare dove sbaglio ?
    Ho provato anche a fare una cosa del genere ma l'evento
    scatta nell'intero body:
    Codice PHP:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"
    <
    html> <head> <title>Untitled Document</title
    <
    meta http-equiv="Content-Type" content="text/html; charset=utf-8"
    <
    script language="javascript"
    alertBox = function () 

        
    alert('caught click'); 

    function 
    myClickListener(event

        if (
    document.addEventListener
        { 
            
    document.addEventListener('click'eventfalse); 
        } 
        else 
        { 
            
    document.attachEvent('onclick'event); 
        } 

    window.onload myClickListener(alertBox); 
    </script> 
    </head> 
    <body>  
    <div id="testdiv">  
    [url="#"]Click here activate the alert method[/url]  
    </div> 
    </body> 
    </html> 
    Ciao
    Without faith, nothing is possible. With it, nothing is impossible
    http://ilwebdifabio.it

  2. #2
    codice:
    ...
        var div=document.getElementById('testdiv');
        div.addEventListener('click',alertBox,false);
    </script>
    </head>
    <body>
        <div id="testdiv"> ...
    guarda che finchè il div non è presente sulla pagina document.getElementById non serve a niente perchè non c'è ....


    inoltre quando assegni un evento non puoi usare le parentesi o inviare paramentri ...

    codice:
    window.onload = myClickListener(alertBox);
    scrivendo questo hai:
    1 - richiamato immediatamente la funzione myClickListener
    2 - assegnato al metodo onload il risultato di tale funzione


    codice:
    function alertBox() {
    	alert('Reacting to Event bubble phase')
    };
    function addMyEvent() {
    	document.getElementById("testdiv").addEventListener("click", alertBox, false); 
    };
    onload = addMyEvent; // non richiami la funzione, la assegni
    ... o se preferisci ...
    codice:
    function alertBox() {alert('Reacting to Event bubble phase')};
    onload = function() {
    	document.getElementById("testdiv").addEventListener("click", alertBox, false); 
    };
    Formaldehyde a new Ajax PHP Zero Config Error Debugger

    WebReflection @WebReflection

  3. #3

    .........

    E si si si dopo aver picchiato
    la testa mi ero proprio accorto
    che il div non c'era debug con alert
    NULL.
    Ti ringrazio molto della dettagliata spiegazione
    adesso me la studio bene avevo risolto
    così:
    Codice PHP:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"
    <
    html> <head> <title>Untitled Document</title
    <
    meta http-equiv="Content-Type" content="text/html; charset=utf-8"
    <
    script language="javascript"
    function 
    alertBox () 

        
    alert('caught click'); 


    window.onload = function()
    {
        if (
    document.addEventListener
        { 
            
    document.getElementById('testdiv').addEventListener('click',alertBox false); 
        } 
        else 
        { 
            
    document.getElementById('testdiv').attachEvent('onclick'alertBox); 
        } 
    }
    </script> 
    </head> 
    <body>  
    <div id="testdiv">  
    [url="#"]Click here activate the alert method[/url]  
    </div> 
    </body> 
    </html> 
    Come avevi suggerito tu.
    Grazie ancora.
    Stammi bene.

    Without faith, nothing is possible. With it, nothing is impossible
    http://ilwebdifabio.it

  4. #4
    codice:
    function DOMize(element) {
    	if(!element.addEventListener){element.addEventListener=function(event,listener){
    		event="on"+event;
    		if(this.attachEvent)	this.attachEvent(event,listener);
    		else 			this[event]=listener;
    	}};
    	if(!element.removeEventListener){element.removeEventListener=function(event,listener){
    		event="on"+event;
    		if(this.detachEvent)	this.detachEvent(event,listener);
    		else 			delete this[event];
    	}};
    	return element;
    };
    
    
    function alertBox() {
    	alert('Reacting to Event bubble phase');
    };
    onload = function() {
    	DOMize(document.getElementById("testdiv")).addEventListener("click", alertBox, false); 
    };
    Formaldehyde a new Ajax PHP Zero Config Error Debugger

    WebReflection @WebReflection

  5. #5

    ..........

    Un bijou thanks so much

    Stammi bene.
    Without faith, nothing is possible. With it, nothing is impossible
    http://ilwebdifabio.it

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.