Visualizzazione dei risultati da 1 a 3 su 3
  1. #1
    Utente di HTML.it L'avatar di strae
    Registrato dal
    Apr 2008
    Messaggi
    407

    Associare funzioni ai tag tramite js

    ciao ragazzi, prendendo spunto dal codice di lightbox:
    Codice PHP:
    if (!document.getElementsByTagName){ return; }
    var 
    anchors document.getElementsByTagName('a');
    var 
    areas document.getElementsByTagName('area');
    // loop through all anchor tags
    for (var i=0i<anchors.lengthi++){
     var 
    anchor anchors[i];
     var 
    relAttribute String(anchor.getAttribute('rel'));
     
    // use the string.match() method to catch 'lightbox' references in the rel attribute
     
    if (anchor.getAttribute('href') && (relAttribute.toLowerCase().match('lightbox'))){
      
    anchor.onclick = function () {myLightbox.start(this); return false;}
     }

    io volevo fare una cosa simile, assegnare delle funzioni javascript ai vari tag in base ai loro attributi.
    quindi, per esempio, vorrei che un mio tag
    <a rel="jsF.provaA" name="a">funzione provaA</a>
    mi esegua la funzione provaA(), invece
    <a rel="jsF.provaB" name="b">funzione provaB</a>
    esegua la funzione provaB().

    ho modificato lo script sopra, fino ad ottenere questo (posto tutto il codice che uso per il mio test, non è molto lungo):
    Codice PHP:
    <script>
    function 
    tagIdentify() {
        if (!
    document.getElementsByTagName){ return; }
        var 
    anchors document.getElementsByTagName('a');
        for (var 
    i=0anchors.lengthi++){
            var 
    anchor anchors[i];
            var 
    relAttribute String(anchor.getAttribute('rel'));
            if(
    relAttribute.match('jsF')){
                if(
    relAttribute.indexOf(".")){
                    
    rayRel relAttribute.split(".");
                    var 
    fn rayRel[1];
                    
    anchor.onclick = function(){ fn(); return false; }
                }
            }
        }
    }

    function 
    provaA(){
        
    alert('funzione A!');
    }
    function 
    provaB(){
        
    alert('funzione B!');
    }

    function 
    getIt(x){
        var 
    document.getElementById(x).onclick;
        
    alert(m);
    }
    </script>
    <div id="pppp" onclick="tagIdentify();">init</div>



    <a rel="jsF.provaA" id="A" class="prova" name="null">funzione provaA</a>

    <a rel="jsF.provaB" id="B" class="prova" name="null">funzione provaB</a>



    <a onclick="getIt('A');" name="eee">get it</a> 
    ..che naturalmente non funziona.
    cliccando sul get it, si vede che a ogni tag viene associata la funzione:
    Codice PHP:
    function () {
        
    fn();
        return 
    false;

    che dà errore..

    ho provato con
    anchor.onclick = fn();
    ma l'errore che mi riporta è 'fn is not a function'... come posso fare?

    spero di essere stato chiaro..
    You HAVE to assume your visitor is a maniac serial killer, out to destroy your application. And you have to prevent it.
    I can accept failure, everyone fails at something - But I can't accept not trying.

  2. #2
    ehr, mi sa che tu non hai molto chiaro il concetto di funzione ed istruzione:

    function empty(){}

    var copyOfEmpty = empty; //funzione

    var returnOfEmpty = empty(); //istruzione

    nel tuo caso cambia il codice così:

    Codice PHP:
    function tagIdentify() {
        if (!
    document.getElementsByTagName){ return; }
        var 
    anchors document.getElementsByTagName('a');
        for (var 
    i=0anchors.lengthi++){
            var 
    anchor anchors[i];
            var 
    relAttribute String(anchor.getAttribute('rel'));
            if(
    relAttribute.match('jsF')){
                if(
    relAttribute.indexOf(".")){
                    
    rayRel relAttribute.split(".");
                    eval(
    'anchor.onclick = '+rayRel[1]+';');
                }
            }
        }



    function 
    getIt(x){
        var 
    document.getElementById(x).onclick;
        
    m();

    Nota: sono partito dal presupposto che quello che sta prima dell'assegnamento funzioni.
    I DON'T Double Click!

  3. #3
    Utente di HTML.it L'avatar di strae
    Registrato dal
    Apr 2008
    Messaggi
    407
    Originariamente inviato da artorius
    ehr, mi sa che tu non hai molto chiaro il concetto di funzione ed istruzione:

    function empty(){}

    var copyOfEmpty = empty; //funzione

    var returnOfEmpty = empty(); //istruzione

    nel tuo caso cambia il codice così:

    Codice PHP:
    function tagIdentify() {
        if (!
    document.getElementsByTagName){ return; }
        var 
    anchors document.getElementsByTagName('a');
        for (var 
    i=0anchors.lengthi++){
            var 
    anchor anchors[i];
            var 
    relAttribute String(anchor.getAttribute('rel'));
            if(
    relAttribute.match('jsF')){
                if(
    relAttribute.indexOf(".")){
                    
    rayRel relAttribute.split(".");
                    eval(
    'anchor.onclick = '+rayRel[1]+';');
                }
            }
        }



    function 
    getIt(x){
        var 
    document.getElementById(x).onclick;
        
    m();

    Nota: sono partito dal presupposto che quello che sta prima dell'assegnamento funzioni.
    si io credevo che fosse un problema di sintassi, che invece di associare la funzione associava un valore (dopo il post ho continuato a fare prove, e mi sono bloccato con
    //anchor.onclick = rayRel[1];
    anchor.onclick = provaA;
    che funziona solo l'ultimo caso.. o meglio, al primo manca solo eval();

    cmq grazie!!
    You HAVE to assume your visitor is a maniac serial killer, out to destroy your application. And you have to prevent it.
    I can accept failure, everyone fails at something - But I can't accept not trying.

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.