我想创建一个包含几个div的网页,每个div包含具有不同实现的相同绘制函数(如通用接口).加载页面后,我想遍历所有div并一个接一个地调用每个绘制函数.
到目前为止,我的页面如下所示:
<html> <head> <Meta http-equiv="content-type" content="text/html; charset=ISO-8859-1"> <script language="javascript" type="text/javascript" src="jquery-1.8.2.js"></script> </head> <body> <script type='text/javascript'> $( document ).ready( function() { // Draw all slots $('div.slot').each(function(i,d) { console.log('slot found: ' + d.id); // d.draw() does not work draw(); }); }); </script> <div class="slot" id="slot1"> <script type='text/javascript'> function draw() { console.log('Here we draw a circle'); }; </script> </div> <div class="slot" id="slot2"> <script type='text/javascript'> function draw() { console.log('Here we do something totally different and draw a rectangle'); }; </script> </div> </body> </html>
不幸的是我不知道如何调用所选div“d”的绘制函数.
现在它只调用最后定义的绘图函数.
更新: