<script type="text/javascript">
function initAnchor()
{
for(var i=1; i<=3; i++)
{
var anchor = document.getElementById('anchor' +i);
anchor.addEventListener('click', function()
{
alert('My id is anchor' +i);
}, false);
}
}
function initAgain()
{
initAnchor();
}
window.addEventListener('load', initAnchor, false);
</script>
<a id='anchor1'>anchor 1</a><br>
<a id='anchor2'>anchor 2</a><br>
<a id='anchor3'>anchor 3</a><br>
function initAnchor()
{
for(var i=1; i<=3; i++)
{
var anchor = document.getElementById('anchor' +i);
anchor.addEventListener('click', function()
{
alert('My id is anchor' +i);
}, false);
}
}
function initAgain()
{
initAnchor();
}
window.addEventListener('load', initAnchor, false);
</script>
<a id='anchor1'>anchor 1</a><br>
<a id='anchor2'>anchor 2</a><br>
<a id='anchor3'>anchor 3</a><br>
using System;
class P
{
static void Main()
{
int i=1;
Anchor[] list = new Anchor[3];
for(; i<=3; i++)
{
list[i-1] = new Anchor();
list[i-1].Clicked += delegate()
{
Console.WriteLine("my id is anchor" +i);
};
}
foreach(Anchor anchor in list)
{
anchor.FireClickEvent();
}
}
class Anchor
{
public event Click Clicked;
public void FireClickEvent()
{
if(Clicked!=null)
Clicked();
}
}
delegate void Click();
}
class P
{
static void Main()
{
int i=1;
Anchor[] list = new Anchor[3];
for(; i<=3; i++)
{
list[i-1] = new Anchor();
list[i-1].Clicked += delegate()
{
Console.WriteLine("my id is anchor" +i);
};
}
foreach(Anchor anchor in list)
{
anchor.FireClickEvent();
}
}
class Anchor
{
public event Click Clicked;
public void FireClickEvent()
{
if(Clicked!=null)
Clicked();
}
}
delegate void Click();
}