动态添加HTML时onclick函数参数传递
onclick函数动态传参
1.参数为数值类型时:
var tmp = 123; var strHTML = "<div onclick=func(" + tmp + ")>点击弹出数据及其类型</div>"; info.append(strHTML); function func(tmp) { alert(typeof tmp + " " + tmp); }
string 123
2.参数为字符串类型时:
var tmp = "abc"; var strHTML = "<div onclick=func('" + tmp + "')>点击弹出数据及其类型</div>"; info.append(strHTML); function func(tmp) { alert(typeof tmp + " " + tmp); }
打印出 string abc
3.传两个及以上的参数时:
var tmp = "abcda"; var type = "xza"; //tmp参数和type参数之间不能有空格 var strHTML = "<div onclick=func('" + tmp + "','" + type + "')>点击弹出数据及其类型</div>"; info.append(strHTML); function func(tmp, type) { alert(tmp + " " + type); }
打印出 abcda xza
4.参数为数组时:
var tmp = ["abc", "ddd", "gg"]; var strHTML = "<div onclick=func('" + tmp + "')>点击弹出数据及其类型</div>"; info.append(strHTML); function func(tmp) { alert(typeof tmp + " " + tmp); }
打印出 string abc,ddd,gg