我的div元素是:
<div id="div"></div>
下面是javascript。:
function makediv() {
var divmy = document.getElementById("div");
var row ='';
var color='#ccc';
for(var i=0;i<2;i++)
{
divmy.innerHTML += " <div class='dynamic' id='inner"+i+"' onclick=searchFilterations(i)>"+i+"</div> ";
color='black';
}
}
function searchFilterations(e){
alert(e);
}
问题是:searchFilterations()函数没有在click上触发。错误是什么? 这里是小提琴: http://jsfiddle.net/cjT4s/4/
答案 0 :(得分:0)
请致电makediv
函数onload
活动
window.onload = makediv;
function makediv() {
var divmy = document.getElementById("div");
var row ='';
var color='#ccc';
for(var i=0;i<2;i++)
{
// divmy.innerHTML += " <div style='width: 100px;height: 30px;background-color:"+color+" ;' onclick=javascript:alert("+i+")></div> ";
divmy.innerHTML += " <div class='dynamic' id='inner"+i+"' onclick=searchFilteration("+i+")>"+i+"</div> ";
color='black';
}
}
function searchFilteration(i){
alert(i);
}
答案 1 :(得分:0)
使用window.searchFilteration = function (i) { ... }
:
makediv();
function makediv() {
var divmy = document.getElementById("div");
var row ='';
var color='#ccc';
for(var i=0;i<2;i++)
{
divmy.innerHTML += " <div class='dynamic' id='inner"+i+"' onclick='searchFilteration("+i+")'>"+i+"</div> ";
color='black';
}
}
window.searchFilteration = function (i){
alert(i);
}