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=0; i<anchors.length; i++){
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=0; i < anchors.length; i++){
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 m = 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..