フォームに 3 つの div 要素を追加し、それぞれのイベント ハンドラーを動的に追加しました。そのdivのIDを示すメッセージをポップアップすることになっています。ただし、どのボックスをクリックしても、最後の div の ID である 10175 がポップアップします。
任意の入力をいただければ幸いです。
<html>
<head>
<style type="text/css">
.empty
{
background-color: green;
}
.safebox
{
margin-left: auto;
margin-right: 20px;
margin-top: 20px;
text-align: center;
float: left;
width: 60px;
height: auto;
}
</style>
<script type="text/javascript">
function ClickMe(elem){
alert(elem.id);
};
window.onload=function(){
var tempdiv = document.createElement('div');
var text = document.createTextNode('0173');
tempdiv.className='safebox empty';
tempdiv.appendChild(text);
tempdiv.setAttribute('id','10173');
tempdiv.appendChild(text);
document.getElementById('pagewarpper').appendChild(tempdiv);
var currentdiv = document.getElementById('10173');
if (currentdiv.addEventListener){
currentdiv.addEventListener('click', function(){ClickMe(currentdiv);}, false);
}
else {
currentdiv.attachEvent('onclick', function(){ClickMe(currentdiv);});
}
var tempdiv = document.createElement('div');
var text = document.createTextNode('0174');
tempdiv.className='safebox empty';
tempdiv.appendChild(text);
tempdiv.setAttribute('id','10174');
tempdiv.appendChild(text);
document.getElementById('pagewarpper').appendChild(tempdiv);
var currentdiv = document.getElementById('10174');
if (currentdiv.addEventListener){
currentdiv.addEventListener('click', function(){ClickMe(currentdiv);}, false);
}
else {
currentdiv.attachEvent('onclick', function(){ClickMe(currentdiv);});
}
var tempdiv = document.createElement('div');
var text = document.createTextNode('0175');
tempdiv.className='safebox empty';
tempdiv.appendChild(text);
tempdiv.setAttribute('id','10175');
tempdiv.appendChild(text);
document.getElementById('pagewarpper').appendChild(tempdiv);
var currentdiv = document.getElementById('10175');
if (currentdiv.addEventListener){
currentdiv.addEventListener('click', function(){ClickMe(currentdiv);}, false);
}
else {
currentdiv.attachEvent('onclick', function(){ClickMe(currentdiv);});
}
}
</script>
</header>
<body>
<form>
<div id="pagewarpper">
</div>
</form>
</body>